stl

difference between foo[i] and foo->at(i) with stl vector

is there any reason why foo = (bar->at(x))->at(y); works but foo = bar[x][y]; does not work, where bar is a vector of vectors (using the c++ stl) the declaration is: std::vector< std::vector < Object * > * > ...

C++ Best way to check if an iterator is valid

Hi! Another newbie question: Is there any way to check if a iterator (whether it is from a vector, a list, a deque...) is (still) dereferencable, i.e has not been invalidated ? If so, which is the best way, in your opinion? (I was 'trying' and 'catching' :/) Thanks again! Edit: An example (which doesn't work) list<int>l; for (i=1; i<...

Is there any reason that the STL does not provide functions to return an iterator via index?

Hi, Is there a reason that the STL does not provide functions to return an iterator into a container via an index? For example, let's say I wanted to insert an element into a std::list but at the nth position. It appears that I have to retrieve an iterator via something like begin() and add n to that iterator. I'm thinking it would be ...

Read a line from file, using stream style

Hi, I have a simple text file, that has following content word1 word2 I need to read it's first line in my C++ application. Following code works, ... std::string result; std::ifstream f( "file.txt" ); f >> result; ... but result variable will be equal to "word1". It should be equal to "word1 word2" (first line of text file) Yes, i ...

Is there an algorithm for moving ranges?

In C++98, I can copy ranges with the std::copy algorithm. std::copy(source.begin(), source.end(), destination.begin()); Is there an algorithm in C++0x that moves the elements from source to destination? Or is std::copy somehow overloaded to accept something like rvalue iterators -- is there even such a thing? The algorithm might look...

STL containers element destruction order

Does ISO C++ standard mandate any sort of destruction order of objects inside STL containers? Are std::list/std::vector/std::map elements destroyed starting from the beginning or the end of the container? Can I rely on std::map storing its elements in std::pairs internally so a key in a pair is destroyed before its value (or vice versa...

fast index for "contains string"

I n my application i have upt o millions of short strings (mostly shorter than 32 characters). I want to implement a search box with a attached list that contains only elements that contain the whole string entered in the search box. How can i prebuild a index to find such strings fast? All sorted STL containers check the whole string. ...

Converting std::list to C friendly type

What's the most elegant way to return a std::list object from a shared lib function (implemented by C++ code) to a C consumer? I know for std::vector, we can return the address of the 1st element of the vector and have the consumer treat it as an array, but std::list is implemented as a linked lis. ...

queue from the stl

I am trying to get the following code to compile using g++ 4.2.1 and am receiving the following errors CODE: #include <iostream> #include <queue> using namespace std; int main (int argc, char * const argv[]) { queue<int> myqueue(); for(int i = 0; i < 10; i++) myqueue.push(i); cout << myqueue.size(); retu...

vector<bool>::push_back bug in GCC 3.4.3?

The following code crashes for me using GCC to build for ARM: #include <vector> using namespace std; void foo(vector<bool>& bools) { bools.push_back(true); } int main(int argc, char** argv) { vector<bool> bools; bool b = false; bools.push_back(b); } My compiler is: arm_v5t_le-gcc (GCC) 3.4.3 (MontaVista 3.4.3-25.0....

Using a STL container to store Treeview data

i'm looking for a C++ STL container class to keep the treeview parent/child node strings but when a node is deleted from tree control, do i have iterate through all the container class elements to find that selected one and then delete it? what's the best to keep the data updated in container? ...

How is C++ stl vector implemented

Hello C++ Gurus, I have been using stl vector a lot, and recently I asked this question to myself,,, "How is stl vector implemented" I had two options:- 1) Linked list:- and then making the api to feel like random access (overloading []) 2) using new for e.g. DATA *temp = new DATA[20]:- I believe they do sth like this but then ...

Which one to use const char[] or const std::string?

Which is better for string literals, standard string or character array? I mean to say for constant strings, say const char name[] = "so"; //or to use const string name = "so"; ...

the patterns used in iterators

I am familiar with the usage of C++ STL iterators, e.g. for(map<pair<int,int>>::iterator it=m.begin(); it!=m.end(); ++it) int a = it->first; int b = it->second; But I don't know the inner details in it. Could some explain to me? Either in C++, Java, C# or Python. ...

C++ STL container and in-place construction

Please consider the following: class CMyClass { public: CMyClass() { printf( "Constructor\n" ); } CMyClass( const CMyClass& ) { printf( "Copy constructor\n" ); } }; int main() { std::list<CMyClass> listMyClass; listMyClass.resize( 1 ); return 0; } It produces the following output: Constructor Copy...

Why can't for_each modify its functor argument?

http://www.cplusplus.com/reference/algorithm/for_each/ Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator(). Its return value, if any, is ignored. According to this article, I expected that for_each actually modifies the objec...

vector and const

Hello Consider this void f(vector<const T*>& p) { } int main() { vector<T*> nonConstVec; f(nonConstVec); } The following does not compile.The thing is that vector<T*> can not be converted to vector <const T*> , and that seems illogically to me , because there exists implicit conversion from T* to const T*. Why is this ? v...

What is std::vector::front() used for?

Sorry if this has been asked before, but I am wondering what the use of std::vector::front() is. Is there a reason to use e.g. myvector.front() rather than myvector[0] or myvector.at(0)? ...

Using STL Allocator with STL Vectors

Here's the basic problem. There's an API which I depend on, with a method using the following syntax: void foo_api (std::vector<type>& ref_to_my_populated_vector); The area of code in question is rather performance intensive, and I want to avoid using the heap to allocate memory. As a result, I created a custom allocator which alloc...

STL Multimap Remove/Erase Values

Hi I have STL Multimap, I want to remove entries from the map which has specific value , I do not want to remove entire key, as that key may be mapping to other values which are required. any help please. ...