stl

Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument?

半世苍凉 提交于 2020-01-10 11:52:17
问题 #include <set> #include <string> #include <cassert> using namespace std::literals; int main() { auto coll = std::set{ "hello"s }; auto s = "hello"s; coll.insert(std::move(s)); assert("hello"s == s); // Always OK? } Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument? 回答1: Explicit and unequivocal NO . Standard doesn't have this guarantee, and this is why try_emplace exists. See notes: Unlike insert or emplace,

Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument?

夙愿已清 提交于 2020-01-10 11:51:28
问题 #include <set> #include <string> #include <cassert> using namespace std::literals; int main() { auto coll = std::set{ "hello"s }; auto s = "hello"s; coll.insert(std::move(s)); assert("hello"s == s); // Always OK? } Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument? 回答1: Explicit and unequivocal NO . Standard doesn't have this guarantee, and this is why try_emplace exists. See notes: Unlike insert or emplace,

Trouble with dependent types in templates

假如想象 提交于 2020-01-10 09:34:30
问题 I'm having trouble with templates and dependent types: namespace Utils { void PrintLine(const string& line, int tabLevel = 0); string getTabs(int tabLevel); template<class result_t, class Predicate> set<result_t> findAll_if(typename set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred) // warning C4346 { set<result_t> result; return findAll_if_rec(begin, end, pred, result); } } namespace detail { template<class result_t, class Predicate> set<result_t> findAll_if_rec(set

Multi-dimensional vector initialization

心已入冬 提交于 2020-01-10 08:30:54
问题 I have following std::vector declaration: std::vector<std::vector<std::vector<int> > > m_input; I am initializing it as follows: m_input.resize (100); m_output.resize(100); for (int i = 0; i < 100; ++i) { m_input [i].resize(100); m_output[i].resize(100); for (int j = 0; j < 100; ++j){ m_input [i][j].resize(100); m_output[i][j].resize(100); } } How can I achieve this via the member initializer list ? 回答1: std::vector<T> has a constructor that takes two arguments, a number of elements and an

Does C# have a std::nth_element equivalent?

别来无恙 提交于 2020-01-10 04:51:05
问题 I'm porting some C++ code to C#. Does C# have an equivalent to std::nth_element() or do I need to roll my own? 回答1: I presume you are looking for an accessor that returns the Nth element of an unordered collection by performing a partial-sort on the collection. This tends to be useful when you have a very large collection and are interested in one of the first elements based on some ordering predicate. To my knowledge, neither the .NET BCL or LINQ extensions offer an equivalent. All of the

what is the time complexity of std::next_permutation() function in c++?

醉酒当歌 提交于 2020-01-10 03:58:05
问题 I wanted to know the time complexity of the next_permutation function. Can I view its code too ? 回答1: See http://www.sgi.com/tech/stl/next_permutation.html: Linear. At most (last - first) / 2 swaps. To see the source code, just look in STL header files for your system. On a Unix-like system, you probably need to look somewhere like /usr/include/c++/4.1.2/bits/stl_algo.h . 来源: https://stackoverflow.com/questions/4972470/what-is-the-time-complexity-of-stdnext-permutation-function-in-c

Rank Tree in C++

喜你入骨 提交于 2020-01-10 03:50:08
问题 We need ADT having search and rank features. That is, in addition to the interface of STL map, a function 'int get_rank(key)' is required. Standard implementation of such function requires supporting and updating an extra integer field in every node of self-balanced searching tree (e.g., in black-red tree, used in STL map/set). But it seems, STL map/set do not do this. We're looking for a solution based on standard containers (STL, Boost) having the best possible Time Complexity: finding

C++ STL next_permutation和prev_permutation

左心房为你撑大大i 提交于 2020-01-10 03:43:53
紫书第187页 next_permutation函数 作用:使排列变成字典序更大的下一个排列; 用法:next_permutation(数组起始地址,数组结尾地址);如果有下一个排列,返回true,否则返回false; 代码: # include <bits/stdc++.h> # define LL long long # define pa pair<int,int> # define lson k<<1 # define rson k<<1|1 # define inf 0x3f3f3f3f //ios::sync_with_stdio(false); using namespace std ; const int N = 200100 ; const int M = 1000100 ; const LL mod = 998244353 ; int main ( ) { ios :: sync_with_stdio ( false ) ; int a [ 11 ] ; for ( int i = 1 ; i <= 10 ; i ++ ) a [ i ] = 10 - i ; sort ( a + 1 , a + 11 ) ; do { for ( int i = 1 ; i <= 10 ; i ++ ) cout << a [ i ] << " " ; cout <<

STL源码学习——Vector(向量)

拥有回忆 提交于 2020-01-09 23:02:48
STL源码学习——Vector(向量)   今天继续看STL源码喵。虽然基本上说vector是最简单的容器了,但其实相对来说我觉得同list比起来,还是list实现方便一些喵~让电脑以人脑的方式工作总比让人脑以电脑的方式工作简单吧喵~   Vectors 包含着一系列连续存储的元素,其行为和数组类似。访问Vector中的任意元素或从末尾添加元素都可以在常量级时间复杂度内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是线性时间复杂度。(C/C++ 语言参考,见第8点)   1 :vector中[]没有检查数组越界,相对应的at()函数检查了。所以at()相对来说较为安全喵~   2 :注意capacity()与max_size()的区别喵~总觉得max_size()这个函数怪怪的。 max_size 1 size_type max_size() const 2 { 3 return size_type(-1) / sizeof(_Tp); 4 } 5 size_type capacity() const 6 { 7 return size_type(_M_end_of_storage._M_data - _M_start); 8 }   3 :vector的swap()成员函数效率为O(1),只是交换了各自的指针。 insert 1 iterator insert

《C++ Primer Plus》第16章 string类和标准模板库 学习笔记

笑着哭i 提交于 2020-01-09 21:23:02
C++提供了一组功能强大的库,这些库提供了很多常见编程问题的解决方案以及简化其他问题的工具string类为将字符串作为对象来处理提供了一种方便的方法。string类提供了自动内存管理动能以及众多处理字符串的方法和函数。例如,这些方法和函数让您能够合并字符串、将一个字符串插入到另一个字符串中、反转字符串、在字符串中搜索字符或姊姊富川以及执行输入和输出操作。 诸如auto_ptr以及C++11新增的shared_ptr和unique_ptr等智能指针模板使得管理由new分配的内存更容易。如果使用这些智能指针(而不是常规指针)来保存new返回的地址,则不比在以后使用删除运算符。智能指针对象过期时,其析构函数将自动调用delete运算符。 STL是一个容器类模板、迭代器类模板、函数对象模板和算法函数模板的集合,它们的设计是一致的,都是基于泛型编程原则的。算法通过使用模板,从而独立于所存储的对象的类型;通过使用迭代器接口,从而独立于容器的类型。迭代器是广义指针。 STL使用术语“概念”來描述一组要求。例如,正向迭代器的概念包含这样的要求,即正向迭代器能 够被解除引用,以便读写,问时能够被递增。概念真正的实现方式被称为概念的“模型”例如,正向迭代器概念可以是常规指针或导航链表的对象。基于其他概念的概念叫作“改进”例如,双向迭代器是正向迭代器概念的改进。