stl

C++ Read File Into hash_map

I'm trying to read in a list of words and save them in a C++ STL hash_map along with their position in the alphabetically sorted file. The idea is later I'll need to be able to tell if a string is a word and whether it comes before or after a different word. ifstream f_dict ("dictionary.txt"); __gnu_cxx::hash_map <const char*, int> dic...

Does std::vector.pop_back() change vector's capacity?

If I allocated an std::vector to a certain size and capacity using resize() and reserve() at the beginning of my program, is it possible that pop_back() may "break" the reserved capacity and cause reallocations? ...

Using a std::map as an associative array

So, I'm using a std::map as an associative array. The map is declared as such: std::map<int, CustomClass*> CustomContainer; Later on, I use the CustomContainer object as an associative array, e.g., CustomClass* pClass = CustomContainer[ID] Josuttis states: If you use a key as the index, for which no element yet exists, a new el...

STL Priority Queue on custom class

I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator<(Node &aNode); ... } Node.cpp #include "...

Why do we need an inserter function call when doing a set_union for a set?

i need to make the call to the set_union function of STL like this: set<int> a1, a2; set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), inserter(a1, a1.begin()); and not set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), a1.begin()); why is that so? ...

STL list_iterator code question (STL 4.0.0)

Can anyone explain why the _List_const_iterator would be using _List_node_base and downcast it to _List_node when needed? -- I think there must be some reason behind this. Thanks struct _List_node_base { _List_node_base* _M_next; ///< Self-explanatory _List_node_base* _M_prev; ///< Self-explanatory // ... }; template<...

combination of a two array ( vector )

Hi, 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...

How do we iterate through all elements of a set while inserting new elements to it?

consider this: // set_iterator.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <iostream> #include <set> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { set<int> a1; set<int> a2; a1.insert(3); a1.insert(4); a1.insert(5); a2.insert(1); a2.insert(2); a2.insert(6); set<int>:...

C++ - Using std::count() with abstract data types ?

Hello, My code is using std::count() on a list of an abstract data type that i have defined. (Sommet or Edge in english). But it doesn't work, although i've overloaded the < and == operators like this : bool operator< (const Sommet &left, const Sommet &right) { if(left.m_id_sommet < right.m_id_sommet) return true; return fals...

vector <unsigned char> vs string for binary data

Which is a better c++ container for holding and accessing binary data? std::vector<unsigned char> or std::string Is one more efficient than the other? Is one a more 'correct' usage? ...

A question about C++ template syntax (STL library source code)

I am reading STL source code right now. Though I understand the meat in what I am reading in stl_list.h, I want to fully understand the following snippet (mainly related to the template syntax, I think). template class _List_base { ... typedef typename _Alloc::template rebind<_List_node<_Tp> >::other _Node_Alloc_type; //(1). ......

How do I escape the const_iterator trap when passing a const container reference as a parameter

I generally prefer constness, but recently came across a conundrum with const iterators that shakes my const attitude annoys me about them: MyList::const_iterator find( const MyList & list, int identifier ) { // do some stuff to find identifier return retConstItor; // has to be const_iterator because list is const } The idea t...

error returning std::set<T>::iterator in template

I'm making a template wrapper around std::set. Why do I get error for Begin() function declaration? template <class T> class CSafeSet { public: CSafeSet(); ~CSafeSet(); std::set<T>::iterator Begin(); private: std::set<T> _Set; }; error: type ‘std::set, std::allocator<_CharT> >’ is not derived ...

memorystream - stringstream, string, others?

hello, i am reading in a binary file via the usual c++/STL/iostream syntax. i am copying the whole content into an dynamically allocated char array and this works fine so far. but since i want to serve parts of the content as lines to another part of the program, i think it would be better/easier to stick to streams because i don't wan...

A Polymorphism Problem

it works when : list<ItemFixed> XYZ::List() { list<Item> items = _Browser->GetMusic(); list<ItemFixed> retItems = _Converter->Convert (items); return retItems; } but not : list<ItemFixed> XYZ::List() { return _Converter->Convert (_Browser->GetMusic()); } Any suggestions? thanks ...

Clarification on lists and removing elements

If I have a list<object*>>* queue and want to pop the first object in the list and hand it over to another part of the program, is it correct to use (sketchy code): object* objPtr = queue->first(); queue->pop_first(); return objPtr; // is this a pointer to a valid memory address now? ? According to the documentation on http://www.cp...

Which STL Container?

I need a container (not necessarily a STL container) which let me do the following easily: Insertion and removal of elements at any position Accessing elements by their index Iterate over the elements in any order I used std::list, but it won't let me insert at any position (it does, but for that I'll have to iterate over all element...

std::unique analogue in Qt?

I have browsed the documentation, but didn't find one. Any ideas? ...

C++ sorting and keeping track of indexes

Using c++, and hopefully the STL, I want to sort a sequence of samples in ascending order, but I also want to remember the original indexes of the newly samples. For example I have a set, or vector, or matrix of samples A : [5, 2, 1, 4, 3] I want to sort these to be B : [1,2,3,4,5], but I also want to remember the original indexes of th...

Compiler warning with nested vectors of depth 3 or more

Hi, I am trying to use a class member that uses nested vectors of depth 3: vector< vector< vector > > classVariable_; However, I then get compiler warnings throughout my code when I try do something as simple as classVariable_.clear(): /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h: In member function `std::vector<_T...