stl

Does a pointer become invalidated if an iterator is invalidated in STL containers

╄→гoц情女王★ 提交于 2019-12-24 11:36:01
问题 I am trying to understand the concept of iterator invalidation in vectors. From some of the reading I have done I have found that if a vector contains say 7 elements and you delete the element on the 5th index then the iterators from 5th element onwards become invalidated. This is because all the elements after the 5th index would need to move up one slot. This makes sense to me however I am a bit confused between the following two cases std::vector<foo> vec {foo{1},foo{2}}; //foo is a simple

How to get char[] to work with std::map

六月ゝ 毕业季﹏ 提交于 2019-12-24 10:30:00
问题 EDIT after answered: < should be provided for std::map . For more information about best practice, go for James McNellis's answer . The code included in this question is poorly written. It is just because I am playing with SPOJ and the input data is strictly valid. The std::string approach is what I chose at first, but it turned out to be not fast enough. Thank you. I know I cannot use char[] directly with map, such as map<char[], int> . Thus I put it in a class. But it still can go through

Can make STL string::c_str() return NULL when it has no string?

北城以北 提交于 2019-12-24 09:48:49
问题 My project has legacy library which consider NULL pointer as empty string. But when I get return data from std::wstring like this, std::wstring strData; const wchar* pStr = strData.c_str(); ASSERT(NULL == pStr); // ASSERT!! pStr is not NULL but pointer which wstring point. Can I make std::string return NULL when it has no string data? Now I wrap every str member variable like this : const wchar* GetName() { // I hate this kinds of wrapping function if (m_Name.empty()) { return NULL; } return

Using push_back() for STL List in C++ causes Access Violation, Crash

荒凉一梦 提交于 2019-12-24 09:21:25
问题 I'm creating a game using my own homemade gaming engine, but I'm running into trouble using lists. I have a structure in my program called BoardState. Each of these structures has a list of BoardState pointers called children. This is because I create a tree of BoardStates for the AI of my game. To aid in the creation of my tree, I have a function called MakeBoard. This function gets passed all the information it needs to create a new board, and then it should add the pointer to that new

SceneKit: export scene as STL file for 3D printing?

我是研究僧i 提交于 2019-12-24 08:37:42
问题 How can you export a SceneKit scene as a STL file (for 3D printing)? The write function from SCNScene does not appear to support the STL format. 回答1: you can use SCNScene 's write(to:options:delegate:progressHandler:) . While the online documentation only mentions Collada and SceneKit file formats, the header documentation states that: macOS 10.10 and lower only supports exporting to .dae files. Starting iOS 10 exporting supports .scn as well as all file formats supported by Model I/O.

How to get combination of two array ( vector ) by STL algorithm?

余生长醉 提交于 2019-12-24 07:41:13
问题 I have v1 and v2 , how should I got a new v like below? v1 = {1,2} v2 = {3,4,5} v = {f(1,3) , f(1,4) , f(1,5) f(2,3) ,f(2,4) ,f(2,5)} I know I could do it using two loops, But If there is more idiomatic way like using STL algorithm? //using two loops for iter1 of v1 for iter2 of v2 v.push_back(f(v1,v2)) EDIT: v1 and v2 not necessary have same size. 回答1: There is no appropriate STL algorithm, but this combination possible to do by a custom function and std::generate: #include <vector> #include

C++ iterating map

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 06:19:45
问题 As known the following code is used to iterate map in C++ for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) { std::cout << itr->first << " => " << itr->second << '\n'; } Where itr is declared as std::map::iterator . The members first and second are declared neither in std::map nor in std::iterator. Then how is it available for access? 回答1: The elements of an std::map are std::pair<key_type, mapped_type> , so de-referencing a map iterator gives you a reference to one

ifstream seekg beyond end does not return eof in VS 2008 Express?

て烟熏妆下的殇ゞ 提交于 2019-12-24 05:56:08
问题 In VS 2005, I have some code that looks like this: ifs.open("foo"); while (!ifs.eof()) { ifs.read(&bar,sizeof(bar)); loc = ifs.tellg(); loc += bar.dwHeaderSize; // four byte boundary padding if ((loc % 4) != 0) loc += 4 - (loc % 4); ifs.seekg(loc,ios::beg); } ifs.close(); The code worked fine in VS 2005, but it fails in VS 2008 Express. From what I can tell, VS 2008 is not returning eof() after the code seeks to the end of the file. Am I missing something? I fixed it by adding an explicit

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

落爺英雄遲暮 提交于 2019-12-24 05:52:23
问题 The title is the main question. The exact scenario (I am 'using namespace std;'): void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) { list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator); } This is the comparator definition: class Substring { // ... class OccurrenceComparator { public: bool operator() (Substring * a, Substring *b); } }; Implementation of the comparator is intuitive and trivial. I am also using a very similar

Looped push_back against resize() + iterator

旧时模样 提交于 2019-12-24 05:26:08
问题 Simple question; what's better and why? out.resize( in.size() ); T1::iterator outit = out.begin(); for( inIt = in.begin() to end, ++inIt, ++outIt ) *outit = *inIt OR out.erase(); for( inIt = in.begin() to end, ++inIt ) out.push_back( inIt ); I'm assuming the memory assignment implicit in push_back is worth avoiding but want to make sure. Thanks EDIT: Thanks for the out = in suggestions guys ;). The actual code I'm playing with is: template//can't stop the browser ignoring th class T1, class