stl

attaching char buffer to vector<char> in STL

What is the correct (and efficient) way of attaching the contents of C buffer (char *) to the end of std::vector? Thanks. ...

Getting the Iterator for an inner STL Container?

Hey guys, I am having trouble trying to get iterators for inner sub-containers. Basically imagine this simplified code: typedef map<string, map<string, map> > double_map; double_map dm; .. //some code here for(double_map::iterator it = dm.begin(); it != dm.end(); it++){ //some code here it->first // string it->second // <...

I can't understand this line - dereferencing an address of private member variable or what?!

I asked a question while ago about accessing the underlying container of STL adapters. I got a very helpful answer: template <class T, class S, class C> S& Container(priority_queue<T, S, C>& q) { struct HackedQueue : private priority_queue<T, S, C> { static S& Container(priority_queue<T, S, C>& q) { ...

STL map onto itself?

I'd like to create a std::map that contains a std::vector of iterators into itself, to implement a simple adjacency list-based graph structure. However, the type declaration has me stumped: it would seem you need the entire map type definition to get the iterator type of said map, like so: map< int, Something >::iterator MyMap_it; /...

Using STL/Boost to find and modify matching elements in a vector

Let's say I have a vector declared like this: struct MYSTRUCT { float a; float b; }; std::vector<MYSTRUCT> v; Now, I want to find all elements of v that share the same a, and average their b, i.e. Say v contains these five elements {a, b}: {1, 1}, {1, 2}, {2, 1}, {1, 3}, {2, 2} I want to get v[0], v[1], v[3] (where a is 1) and av...

c++ map find() to possibly insert(): how to optimize operations?

Hi, I'm using the STL map data structure, and at the moment my code first invokes find(): if the key was not previously in the map, it calls insert() it, otherwise it does nothing. map<Foo*, string>::iterator it; it = my_map.find(foo_obj); // 1st lookup if(it == my_map.end()){ my_map[foo_obj] = "some value"; // 2nd lookup }else{ ...

Find largest and second largest element in a range

How do I find the above without removing the largest element and searching again? Is there a more efficient way to do this? It does not matter if the these elements are duplicates. ...

Read a password from std::cin

I need to read a password from standard input and wanted std::cin not to echo the characters typed by the user... How can I disable the echo from std::cin? here is the code that I'm currently using: string passwd; cout << "Enter the password: "; getline( cin, passwd ); Edit: I'm looking for a OS agnostic way to do this. Here there a...

Problem solving in C++ with STL

I am preparing for a programming competition in witch we solve programming problems in c++. Looking at the former year solutions, they seem quite easy (not more than ~30 lines of code). I realised that they are widely using the STL for easy manipulating - vectors, sets, maps, lists and also the algorithms available in STL. Any site fo...

How to use bind1st and bind2nd ?

Hi, I would like to learn how to use binding functions. Here is the idea: I have this function which takes to parameters: void print_i(int t, std::string separator) { std::cout << t << separator; } And I would like to do: std::vector<int> elements; // ... for_each(elements.begin(), elements.end(), std::bind2nd(print_i, '\n')...

A confusion about parallel_accumulate in C++ Concurrency in action

In the following example (Chapter 2), Anthony Williams is trying to parallelize the standard accumulate function. my question is why is he doing this: unsigned long const max_threads=(length+min_per_thread-1)/min_per_thread; why add length and subtract 1? why not just: unsigned long const max_threads=length/min_per_thread; ....

How do I find an element position in std::vector?

I need to find an element position in an std::vector to use it for referencing an element in another vector: int find( const vector<type>& where, int searchParameter ) { for( int i = 0; i < where.size(); i++ ) { if( conditionMet( where[i], searchParameter ) ) { return i; } } return -1; } // caller: c...

Is end() required to be constant in an STL map/set?

§23.1.2.8 in the standard states that insertion/deletion operations on a set/map will not invalidate any iterators to those objects (except iterators pointing to a deleted element). Now, consider the following situation: you want to implement a graph with uniquely numbered nodes, where every node has a fixed number (let's say 4) of neig...

C++ STL containers: what's the difference between deque and list?

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically. Is that correct?? ...

Std::vector is initialized to garbage. Weird behavior. Any ideas on what's up?

My class has this member. std::vector<AvaWrapper> m_controls; In my constructor I call m_controls.clear() Then I call a member function that does m_controls.clear() again but it blows up with an assert. The debugger shows that m_controls has a half million or more entries though none of them are valid cause the debugger shows ...

How to create a container of noncopyable elements

Is there a way use STL containters with non-copyable elements? something like this: class noncopyable { noncopyable(noncopyable&); const noncopyable& operator=(noncopyable&); public: noncopyable(){}; }; int main() { list<noncopyable> MyList; //error C2248: 'noncopyable::noncopyable' : cannot access private member decla...

Iterate keys in a C++ map

Is there a way to iterate over the keys, not the pairs of a C++ map? ...

Using a struct member in STL algorithms

#include <iostream> #include <vector> #include <iterator> using namespace std; struct Point { int x; int y; Point(int x, int y) : x(x), y(y) {} }; int main() { vector<Point> points; points.push_back(Point(1, 2)); points.push_back(Point(4, 6)); vector<int> xs; for(vector<Point>::ite...

Why there is no std::copy_if algorithm?

Is there any specific reason for not having std::copy_if algorithm in C++ ? I know I can use std::remove_copy_if to achieve the required behavior. I think it is coming in C++0x, but a simple copy_if which takes a range, a output iterator and a functor would have been nice. Was it just simply missed out or is there some other reason behin...

how to append a list<T> object to another

in C++, I have two list<T> objects A and B and I want to add all the members of B to the end of A. I've searched a few different sources and haven't found a simple solution (e.i. A.append(B);) and this surprises me a bit. What is the best way to do this? As it happens, I don't care about B after this (it gets deleted in the very next l...