stl

What's the simplest way to create an STL - identity map?

I'd like to initialize a map - object "id" with identities from 0 to n-1, i.e. id[0] = 0 id[1] = 1 . . id[n-1] = n-1 Is there a simple way - a one-liner, a method inside the map-object, simply something really simple - that does that? ...

What is the best data structure for representing nodes in 3D space?

Hello! ... and thanks for reading... I'm still learning the ropes so please be forgiving... ;-) I am writing a function that meshes a solid in space. The mesh is done by using objects of a "Node" class and each node is represented by: int id double p double r Initially I thought that a map would be the way to go: with a map I can m...

Problems with const set&. Compiler/STL bug or non-portable usage?

Are there any language lawyers in the house? Should the following code compile? include <set> bool fn( const std::set<int>& rSet ) { if ( rSet.find( 42 ) != rSet.end() ) return true; return false; } On one of the platforms (Sun Workshop) this does not compile. It reports that the find function returned an iterator and the end fu...

Should I become proficient with STL libraries before learning BOOST alternatives?

Does it make sense to restrict yourself to the STL libraries when learning C++ and then tackle boost and its additions after you have become fairly proficient with vanilla C++? Or should you dive right into BOOST while learning C++? ...

Reference to value of STL map element?

Is it OK to pass to function a reference to the value of map element, and to modify it there? foo(string & s) { s = "xyz"; } map<int, string> m; m[1] = "abc"; foo(m[1]); // <-- Is it ok? Will m[1] be "xyz" after this call? Thank you. ...

Get bytes from a std::vector<bool>

I have something like the following, and after populating it with a arbitrary number of bits, I need to get the bytes to write out to a file. I don't see a way to do this and it seems useful, so I must be missing something. Any idea's? std::vector<bool> a; a.push_back(true); a.push_back(false); a.push_back(false); a.push_back(true); a...

Can I disable exceptions in STL?

Hello, I want to disable exceptions in my C++ aplication, compiled under MSVC. I hve switched the option Enable C++ exceptions to NO, but I get warnings telling me to use the option /Ehsc, which I dont want to. I do not have try/catch blocks in my code, but I use STL. I have found that using macro definition _HAS_EXCEPTIONS=0 should...

C++ serialization of complex data using Boost

Hi, I have a set of classes I wish to serialize the data from. There is a lot of data though, (we're talking a std::map with up to a million or more class instances). Not wishing to optimize my code too early, I thought I'd try a simple and clean XML implementation, so I used tinyXML to save the data out to XML, but it was just far to...

Constructor doesn't work for class inherited from std::string

when doing #include <string> class MyString : public std::string { public: MyString() {} }; But the usage below: MyString s = "Happy day"; MyString s("Happy Day"); MyString s = (MyString)"Happy day"; neither of them works. It seems that there's something to do with constructors/operators declaration/overridding, but can ...

STL-like vector with arbitrary index range

What I want is something similar to STL vector when it comes to access complexity, reallocation on resize, etc. I want it to support arbitrary index range, for example there could be elements indexed from -2 to +7 or from +5 to +10. I want to be able to push_front efficiently. Also I want two-way resize... I know I could write something...

map complex find operation

I want to do the following: Define a map between a string and any kind of object (may be a list, integer - anything). The keys to the map can be as follow (the values are, again, not important): "AAA/123" ==> 1 "AAA/" ==> 2 "BBB/" ==> 3 "CCC/*" ==> 4 "CCC/123" ==> 5 Now, the trick is I want to find the right values given the following st...

What is the overhead cost of an empty vector?

What is the memory overhead of having an empty vector vs having a pointer to a vector? Option A: std::vector<int> v; Option B: std::vector<int> *v = NULL; I believe that option B takes 1 32 bit pointer (assuming 32 bit here) How much memory does the empty 'v' take up? ...

What are some C++ Standard Library usage best practices?

I'm learning C++ and the book I'm reading (The C++ Programming Language) says to not reinvent the wheel, to rely on the standard libraries. In C, I often end up creating a linked list, and link list iteration over and over again (maybe I'm doing that wrong not sure), so the ideas of containers available in C++, and strings, and algorithm...

C++ Vector

Look this code(and forgive the miss of knowlegde).It outputs errors that I couldnot solve.I need to declare a vector of elements of struct C,but I need the number of elements be i(a input of type int).I also tried others aproachs but in all of them I recieved an error(cannot convert C to int,etc).How can I do this? # include < iostream...

Shift operations

I saw the following posted by one of the fellow stackoverflower and it sort of dumbfounds me. Would someone explain the shifting operations in the following code snippet: std::vector<bool> a; a.push_back(true); a.push_back(false); //... for (auto it = a.begin(); it != a.end();) // see 0x for meaning of auto { unsigned b = 0; f...

How to best write out a std::vector < std::string > container to a HDF5 dataset?

Given a vector of strings, what is the best way to write them out to a HDF5 dataset? At the moment I'm doing something like the following: const unsigned int MaxStrLength = 512; struct TempContainer { char string[MaxStrLength]; }; void writeVector (hid_t group, std::vector<std::string> const & v) { // // Firstly...

Why is a C++ Vector called a Vector?

The question's pretty self-explanatory really. I know vaguely about vectors in maths, but I don't really see the link to C++ vectors. Thanks! ...

Assertion error in std:vector used in std::set_difference

I am trying to find the set difference of two vectors, so i do something like this: std::vector<sha1_hash> first_vec, second_vec, difference_vec; // populate first_vec and second_vec ... std::sort(first_vec.begin(),first_vec.end()); std::sort(second_vec.begin(),second_vec.end()); std::set_difference(first_vec.begin(),first_vec.end(),...

memcmp sort

I have a single buffer, and several pointers into it. I want to sort the pointers based upon the bytes in the buffer they point at. qsort() and stl::sort() can be given custom comparision functions. For example, if the buffer was zero-terminated I could use strcmp: int my_strcmp(const void* a,const void* b) { const char* const one ...

How do I return hundreds of values from a C++ function?

In C++, whenever a function creates many (hundreds or thousands of) values, I used to have the caller pass an array that my function then fills with the output values: void computeValues(int input, std::vector<int>& output); So, the function will fill the vector output with the values it computes. But this is not really good C++ style...