stl

boost::shared_ptr STL container question

Assume I have a class foo, and wish to use a std::map to store some boost::shared_ptrs, e.g.: class foo; typedef boost::shared_ptr<foo> foo_sp; typeded std::map<int, foo_sp> foo_sp_map; foo_sp_map m; If I add a new foo_sp to the map but the key used already exists, will the existing entry be deleted? For example: foo_sp_map m; vo...

C++ STL: should I store entire objects, or pointers to objects?

Designing a new system from scratch. I'll be using the STL to store lists and maps of certain long-live objects. Question: Should I ensure my objects have copy constructors and store copies of objects within my STL containers, or is it generally better to manage the life & scope myself and just store the pointers to those objects in m...

gcc3.3 undefined reference to std::_Rb_tree<>::insert_unique

I'm building a shared library with g++ 3.3.4. I cannot link to the library because I am getting ./BcdFile.RHEL70.so: undefined symbol: _ZNSt8_Rb_treeIjjSt9_IdentityIjESt4lessIjESaIjEE13insert_uniqueERKj Which c++filt describes as std::_Rb_tree<unsigned int, unsigned int, std::_Identity<unsigned int>, std::less<unsigned int>, std::a...

Debugging C++ STL containers in Windbg

Windbg fans claim that it is quite powerful and I tend to agree. But when it comes to debugging STL containers, I am always stuck. If the variable is on the stack, the !stl extension sometimes figures it out, but when a container with a complex type (e.g. std::vector<TemplateField, std::allocator<TemplateField> >) is on the heap or part ...

Help improve this INI parsing code

This is something simple I came up with for this question. I'm not entirely happy with it and I saw it as a chance to help improve my use of STL and streams based programming. std::wifstream file(L"\\Windows\\myini.ini"); if (file) { bool section=false; while (!file.eof()) { std::wstring line; std::getline(file, line); ...

Using boost::random as the RNG for std::random_shuffle

I have a program that uses the mt19937 random number generator from boost::random. I need to do a random_shuffle and want the random numbers generated for this to be from this shared state so that they can be deterministic with respect to the mersenne twister's previously generated numbers. I tried something like this: void foo(std::ve...

UTF8 to/from wide char conversion in STL

Is it possible to convert UTF8 string in a std::string to std::wstring and vice versa in a platform independent manner? In a Windows application I would use MultiByteToWideChar and WideCharToMultiByte. However, the code is compiled for multiple OSes and I'm limited to standard C++ library. ...

Disk-backed STL container classes?

I enjoy developing algorithms using the STL, however, I have this recurring problem where my data sets are too large for the heap. I have been searching for drop-in replacements for STL containers and algorithms which are disk-backed, i.e. the data structures on stored on disk rather than the heap. A friend recently pointed me toward...

C++ last loop iteration (STL map iterator)

I'm trying to figure out the best way to determine if I'm in the last iteration of a loop over a map in order to do something like the following: for (iter = someMap.begin(); iter != someMap.end(); ++iter) { bool last_iteration; // do something for all iterations if (!last_iteration) { // do something for all but the...

How can currying be done in C++?

What is currying? How currying can be done in c++? Please Explain binders in STL container? ...

Idiomatic C++ for reading from a const map

For an std::map<std::string, std::string> variables, I'd like to do this: BOOST_CHECK_EQUAL(variables["a"], "b"); The only problem is, in this context variables is const, so operator[] won't work :( Now, there are several workarounds to this; casting away the const, using variables.count("a") ? variables.find("a")->second : std::stri...

Optimising C++ 2-D arrays

I need a way to represent a 2-D array (a dense matrix) of doubles in C++, with absolute minimum accessing overhead. I've done some timing on various linux/unix machines and gcc versions. An STL vector of vectors, declared as: vector<vector<double> > matrix(n,vector<double>(n)); and accessed through matrix[i][j] is between 5% and 100...

How can I expose iterators without exposing the container used?

Hello world! I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help. I would like to expose an iterator for a class like this: template <class T> class MyContainer { public: /...

How can I read binary data from wfstream?

Hello world! I have a slight problem reading data from file. I want to be able to read wstring's, aswell as a chunk of raw data of arbitrary size (size is in bytes). std::wfstream stream(file.c_str()); std::wstring comType; stream >> comType; int comSize; stream >> comSize; char *comData = new char[comSize]; memset(comData, 0, comS...

Switching from std::string to std::wstring for embedded applications?

Up until now I have been using std::string in my C++ applications for embedded system (routers, switches, telco gear, etc.). For the next project, I am considering to switch from std::string to std::wstring for Unicode support. This would, for example, allow end-users to use Chinese characters in the command line interface (CLI). What ...

Where can I learn about the C++ Standard Template Library (STL)?

This question is a lot like Good book for learning the C++ standard template library? but aimed at the cash-strapped student type. I remember searching for some reference material about the STL a while back, but I couldn't find any good links. Are there any worthwile free tutorials / references / best-practises available? ...

What is the most efficient way to (re)initialise a vector to a certain length with initial values

Hi all, As a function argument I get a vector<double>& vec (an output vector, hence non-const) with unknown length and values. I want to initialise this vector to a specific length n with all zeroes. This will work vec.clear(); vec.resize( n, 0.0 ); And this will work as well: vec.resize( n ); vec.assign( n, 0.0 ); Is the second...

To STL or !STL, that is the question...

Unquestionably, I would choose to use the STL for most C++ programming projects. The question was presented to me recently however, "Are there any cases where you wouldn't use the STL?"... The more I thought about it, the more I realized that perhaps there SHOULD be cases where I choose not to use the STL... For example, a really large,...

STL non-copying wrapper around an existing array?

Is it possible to create an STL-like container, or even just an STL-style iterator, for an existing array of POD-type elements? For example, suppose I have an array of ints. It would be convenient to be able to call some of the STL functions, such as find_if, count_if, or sort directly on this array. Non-solution: copying the entire a...

With a STL map/set/multiset/multimap, How to find the first value greater than or equal to the search key?

Suppose I have a set of values, stored in a std::set: {1, 2, 6, 8} and I have a search key, say, 3. I want to put 3 into a function and get the first value greater than or equal to 3, in this case I would want to get 6. The find() function provided in map/set/multimap/and set will, of course, return the end iterator for this case. Is ...