stl

Does the new C++ Standard provide new containers?

With C++ STL being updated, will there ever be a set number of containers. Edit: When it comes to containers, Will there be new addition to the library in addition vectors, lists etc.. ...

why STL header files have no extension?

I got this basic doubt. The STL header doesn't have .h extension. #include <vector> #include <map> Is there is any specific reason behind this? Anybody knows history behind this, please share. EDIT: @GMan found Michael Burr's answer which addresses this question. ...

Order like a list but access by a key?

I used list to place cities into a trip. Then I iterate over the list to display the trip itinerary. I would like to access the cities by the name rather than by the trip order. So, I thought I could use a map rather than a list but the key determines the order. I would still like to control the order of the sequence but be able to acces...

STL container assignment and const pointers.

This compiles: int* p1; const int* p2; p2 = p1; This does not: vector<int*> v1; vector<const int*> v2; v2 = v1; // Error! v2 = static_cast<vector<const int*> >(v1); // Error! What are the type equivalence rules for nested const pointers? I thought the conversion would be implicit. Besides, I'd rather not implement point-wise as...

std::string length() and size() member functions

I was reading the answers for this question and found that there is actually method called length() for strings (I always used size()). Is there any specific reason for having this method in string class ? I read both MSDN and CppRefernce and they seem to indicate that there is no difference between size() and length(). If that is so, is...

Benefit of slist over vector?

What I need is just a dynamically growing array. I don't need random access, and I always insert to the end and read it from the beginning to the end. slist seems to be the first choice, because it provides just enough of what I need. However, I can't tell what benefit I get by using slist instead of vector. Besides, several materials I...

is there any function in C++ that calculates a fingerprint or hash of a string that's guaranteed to be at least 64 bits wide?

is there any function in C++ that calculates a fingerprint or hash of a string that's guaranteed to be at least 64 bits wide? I'd like to replace my unordered_map<string, int> with unordered_map<long long, int>. Given the answers that I'm getting (thanks Stack Overflow community...) the technique that I'm describing is not well-known. ...

c++ back_inserter for a set?

I guess this is a simple question. I need to do something like this: std::set<int> s1, s2; s1 = getAnExcitingSet(); transform(s1.begin(), s1.end(), std::back_inserter(s2), ExcitingUnaryFunctor()); Of course, back_inserter doesn't work since there's no push_back. std::inserter also needs an iterator? I haven't used inserter so I'm not ...

what happens when you modify an element of an std::set?

Like the question says, If I change an element of an std::set, for example, through an iterator, I know it is not "reinserted" or "resorted", but is there any mention of if it triggers undefined behavior? For example, I would imagine insertions would screw up. Is there any mention of specifically what happens? ...

Is the following code using std::set "legal"?

I have this code: set<int>::iterator new_end = set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), set1.begin()); set1.erase(new_end, set1.end); It compiles and runs fine in visual studio. However, in a previous question, people stat...

Why does push_back or push_front invalidate a deque's iterators?

As the title asks. My understanding of a deque was that it allocated "blocks". I don't see how allocating more space invalidates iterators, and if anything, one would think that a deque's iterators would have more guarantees than a vector's, not less. ...

find_if function build problems

I'm trying to build the following block of code in a 1-file .cpp file: #include <iostream> #include <algorithm> using namespace std; class test { public: int a[10]; int index; test(); ~test(); bool equals(int p); void search(); }; test::test() { int temp[10] = {4, 9, 5, 6, 9, 10, 9, 255, 60, 0}; me...

Confusion on iterators invalidation in deque

I'm bit confused regarding iterator invalidation in deque. (In the context of this question) Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterator...

Further std::set woes

I asked this question earlier. I am intrigued by std::set but I have another confusing scenario. Namely, is the following code legal, portable c++ for T=std::vector and T=std::set: template <typename T> void remove_elements(T& collection, int removal_value) { typename T::iterator new_end = std::remove(collection.begin(), c...

Vector of pointers template clearing function fails to compile with "undefined reference" message

Hello all, For a program of mine I made a small function to clear the various std::vectors of pointers that I have. template <class S> void clearPtrVector(std::vector<S*> &a,int size) { for(size_t i = 0; i < size; i++) delete a[i]; a.clear(); } I must have done something wrong here though since when calling this fun...

C++ Templates: Coding error or compiler bug?

I'm trying to use templates to get std:list of items, where each item has a pointer to the list which contains it, but I keep hitting a compiler message. Here's a very stripped down version of the code. template <class E> class Item { public: E* owner; // pointer to list that owns us. }; template <class E> class BaseList: p...

How to create a structure which contains a list of itself

I want to create a structure which contains a list of same structure like this. #include <list> struct Url { CString strUrl; std::list<Url> children; }; void main() { Url u1, u2; u1.children.push_back(u2); } This code is not compiling. But when I replace std::list with std::vector it is working fine. How can I make th...

Is there any real risk to deriving from the C++ STL containers?

The claim that it is a mistake ever to use a standard C++ container as a base class surprises me. If it is no abuse of the language to declare ... // Example A typedef std::vector<double> Rates; typedef std::vector<double> Charges; ... then what, exactly, is the hazard in declaring ... // Example B class Rates : public std::vector<...

Why can't I make a vector of references?

When I do this: std::vector<int> hello; Everything works great. However, when I make it a vector instead: std::vector<int &> hello; I get horrible errors like "error C2528: 'pointer' : pointer to reference is illegal". I want to put a bunch of references to structs into a vector, so that I don't have to meddle with pointers. Why i...

Removing map element by value

I'll keep this brief. I am trying to keep a map between strings and object pointers, and as such, I use std::map. I have a manager that's a global class that keeps track of the map, and whenever an object's destructor is called, it tells the manager that it has been deleted. The only way I can think of is to search through the map for ...