stl

STL的继承

随声附和 提交于 2019-12-30 03:11:12
网上的观点都不赞成继承STL. SO上的一篇问答: Is it okay to inherit implementation from STL containers, rather than delegate? The risk is deallocating through a pointer to the base class ( delete , delete[] , and potentially other deallocation methods). Since these classes ( deque , map , string , etc.) don't have virtual dtors, it's impossible to clean them up properly with only a pointer to those classes: struct BadExample : vector<int> {}; int main() { vector<int>* p = new BadExample(); delete p; // this is Undefined Behavior return 0; } That said, if you're willing to make sure you never accidentally do this,

Efficiently initialise std::set with a sequence of numbers

喜你入骨 提交于 2019-12-30 02:44:47
问题 An obvious (naive?) approach would be: std::set<int> s; for (int i = 0; i < SIZE; ++i) { s.insert(i); } That's reasonable readable, but from what I understand, not optimal since it involves repeatedly searching for the insertion position and does not take advantage of the fact that the input sequence is already sorted. Is there a more elegant/efficient (or a de facto) way of initialising an std::set with a sequence of numbers? Or, more generically, how does one efficiently insert an ordered

How to do std::string indexof in C++ that returns index of matching string?

流过昼夜 提交于 2019-12-30 02:43:28
问题 I'm looking for a string indexof function from the std namespace that returns an integer of a matching string similar to the java function of the same name. Something like: std::string word = "bob"; int matchIndex = getAString().indexOf( word ); where getAString() is defined like this: std::string getAString() { ... } 回答1: Try the find function. Here is the example from the article I linked: string str1( "Alpha Beta Gamma Delta" ); string::size_type loc = str1.find( "Omega", 0 ); if( loc !=

Difference in performance between map and unordered_map in c++

独自空忆成欢 提交于 2019-12-30 01:46:27
问题 I have a simple requirement, i need a map of type . however i need fastest theoretically possible retrieval time. i used both map and the new proposed unordered_map from tr1 i found that at least while parsing a file and creating the map, by inserting an element at at time. map took only 2 minutes while unordered_map took 5 mins. As i it is going to be part of a code to be executed on Hadoop cluster and will contain ~100 million entries, i need smallest possible retrieval time. Also another

Traverse a List Using an Iterator?

心不动则不痛 提交于 2019-12-30 01:37:09
问题 I need sample for traversing a list using C++. 回答1: The sample for your problem is as follows #include <iostream> #include <list> using namespace std; typedef list<int> IntegerList; int main() { IntegerList intList; for (int i = 1; i <= 10; ++i) intList.push_back(i * 2); for (IntegerList::const_iterator ci = intList.begin(); ci != intList.end(); ++ci) cout << *ci << " "; return 0; } 回答2: To reflect new additions in C++ and extend somewhat outdated solution by @karthik, starting from C++11 it

In-place C++ set intersection

坚强是说给别人听的谎言 提交于 2019-12-30 00:52:29
问题 The standard way of intersecting two sets in C++ is to do the following: std::set<int> set_1; // With some elements std::set<int> set_2; // With some other elements std::set<int> the_intersection; // Destination of intersect std::set_intersection(set_1.begin(), set_1.end(), set_2.begin(), set_2.end(), std::inserter(the_intersection, the_intersection.end())); How would I go about doing an in-place set intersection? That is, I want set_1 to have the results of the call to set_intersection.

How can I display the content of a map on the console?

限于喜欢 提交于 2019-12-30 00:15:36
问题 I have a map declared as follows: map < string , list < string > > mapex ; list< string > li; How can I display the items stored in the above map on the console? 回答1: Well it depends on how you want to display them, but you can always iterate them easily: typedef map<string, list<string>>::const_iterator MapIterator; for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) { cout << "Key: " << iter->first << endl << "Values:" << endl; typedef list<string>::const_iterator

How can I display the content of a map on the console?

做~自己de王妃 提交于 2019-12-30 00:15:09
问题 I have a map declared as follows: map < string , list < string > > mapex ; list< string > li; How can I display the items stored in the above map on the console? 回答1: Well it depends on how you want to display them, but you can always iterate them easily: typedef map<string, list<string>>::const_iterator MapIterator; for (MapIterator iter = mapex.begin(); iter != mapex.end(); iter++) { cout << "Key: " << iter->first << endl << "Values:" << endl; typedef list<string>::const_iterator

How do I use 3 and 4-byte Unicode characters with standard C++ strings?

社会主义新天地 提交于 2019-12-30 00:05:13
问题 In standard C++ we have char and wchar_t for storing characters. char can store values between 0x00 and 0xFF . And wchar_t can store values between 0x0000 and 0xFFFF . std::string uses char , so it can store 1-byte characters only. std::wstring uses wchar_t , so it can store characters up to 2-byte width. This is what I know about strings in C++. Please correct me if I said anything wrong up to this point. I read the article for UTF-8 in Wikipedia, and I learned that some Unicode characters

STL 容器中end()含义

懵懂的女人 提交于 2019-12-29 16:05:48
容器的end()方法,返回一个迭代器,需要注意:这个迭代器不指向实际的元素,而是表示末端元素的下一个元素,这个迭代器起一个哨兵的作用,表示已经处理完所有的元素。 因此,在查找的时候,返回的迭代器,不等于end(),说明找到了目标。等于end(),说明检查了所有元素,没有找到目标。 来源: CSDN 作者: Tony_Xian 链接: https://blog.csdn.net/boiled_water123/article/details/103752965