stl

index or position in std::set

I have a std::set of std::string. I need the "index" or "position" of each string in the set, is this a meaningful concept in the context? I guess find() will return an iterator to the string, so my question might be better phrased as : "How do I convert an iterator to a number?". ...

How to use std::wstring with std::istringstream?

I am trying to write a template function which will extract the value of the given datatype from the given string. I came up with something like this: template<class T> static T getValue(const CString& val_in) { std::wstring value = val_in; std::istringstream iss; iss.str(value); T val = T(); iss>>va...

What is the performance penalty of operator overloading STL

I like STL a lot. It makes coding algorithms very convenient since it provides you will all the primitives like parition, find, binary_search, iterators, priority_queue etc. Plus you dont have to worry about memory leaks at all. My only concern is the performance penalty of operator overloading that is necessary to get STL working. For ...

Why no compiler enforcement in const_iterator

Consider the following code : #include <vector> #include <iostream> class a { public: int i; void fun() { i = 999; } void fun() const { std::cout << "const fun" << std::endl; } }; const a* ha() { return new a(); } int main() { std::vector<a *> v; v.push_back(new a()); // cannot convert from 'const a *' to...

STL container's read-only operations

Need advice here: which of the STL container's operations are considered read-only? Take vector<int> as example, would it be safe to say that any operation that does not alter the underlying int data is read-only? I am writing a multi-threaded program, but not too sure if it is thread-safe to pass container by reference/pointer. Between...

Making map::find operation case insensitive

Does the map::find method support case insensitive search? I have a map as follows map<string,vector<string> > directory; and want the below search to ignore case. directory.find(search_string); ...

Counting keys in a hash_multimap

What is the most efficient way to count all the different keys in a hash_multimap? E.g. if I have a already filled hash_multimap, (e.g. a container where you can store multiple entities with a same key) how can i retrieve the set of keys? ...

How to use std::transform with templates

I am struggling to find out why I can't get transform to work with a template class. Here's a simplified version of the template class : template<typename T> class base { public : base() : all_() {} ~base() {} public: bool add(T t) { typename vector<T>::iterator itr = lower_bound(all_.begin(), all_.end(), t); i...

Finding a nonexisting key in a std::map

Is there a way to find a nonexisting key in a map? I am using std::map<int,myclass>, and I want to automatically generate a key for new items. Items may be deleted from the map in different order from their insertion. The myclass items may, or may not be identical, so they can not serve as a key by themself. During the run time of th...

std::map find doesn't work properly

std::map.find() is intended to return an map::iterator to an element it found if there is some or to the end() if not. I get BadPtr returned to me. The exactly same construct works fine in the other part of code. What is this? class OntologyContainer { map<string, OntologyClass*> data; OntologyClass* last_added; public: clas...

How to use unordered_set in STL?

Hi, I am in need of a hash_map class in C++(STL). Primary operation is to put pair in the set and then check if it exists or not. I am unable to find a sample code which does it to know if what I am declaration correctly or not. #include <iostream> #include <hash_map> using namespace std; using namespace __gnu_cxx; typedef pair<int,...

Does the hash<char*> function in STL give 1-1 mapping between char* and size_t ?

I have a pair I know the value of the pair.first cannot be more than 1000. I also know that the pair.second , the string, is always 1 word. Never more than 1 word. So, to construct the Hash value for the pair I am doing the following: pair<int,string> p; hash<char*> H; hash_vale = H(p.second)*1000 + p.first; I think this will give uni...

Help me understand std::erase

In the book 'C++ In A Nutshell', there is the following example code std::vector<int> data ... std::erase(std::remove(data.begin(), data.end(), 42), data.end()); I thought that 'erase' was a member function, so shouldn't that be 'data.erase' rather than 'std::erase'? Is there some way the c++ compiler can tell what member you wanted...

Yield from C# to C++, dealing with containers

Hi there. Actually, I have a design question here. Its very simple but the point is: I have one C++ class that has a STL vector declared as a private member. But the clients of that class need to iterate over this vector. In C# we have a very handy statement, the Yield, that in cases like that, you write a function returning an IEnu...

C++ map insertion and lookup performance and storage overhead

I would like to store a mapping of an integer key to a float value in-memory. I have roughly 130 million keys (and, accordingly, 130 million values). My focus is on lookup performance -- I have to do many, many millions of lookups. The C++ STL library has a map class for associative arrays of this sort. I have several questions abou...

Iterating over all pairs of elements in std-containers (C++)

What's the best way to iterate over all pairs of elements in std container like std::list, std::set, std::vector, etc.? Basically to do the equivalent of this, but with iterators: for (int i = 0; i < A.size()-1; i++) for(int j = i+1; j < A.size(); j++) cout << A[i] << A[j] << endl; ...

two dimensional array using vector in cpp

Hi, I want to create 2D array using vector. But, when I do this, I get seg fault. Can anyone please explain what I am doing wrong, and possible solution for this problem. I made everything public since I dont want to deal with getters and setters now. I want to get the concept of 2D array clear. Thanks. #include <iostream> #include <v...

Why does compiler generate error?

Why does compiler generate error? template<class T> void ignore (const T &) {} void f() { ignore(std::endl); } Compiler VS2008 gives the following error: cannot deduce template argument as function argument is ambiguous. ...

Cost of using std::map with std::string keys vs int keys?

I know that the individual map queries take a maximum of log(N) time. However I was wondering, I have seen a lot of examples that use strings as map keys. What is the performance cost of associating a std::string as a key to a map instead of an int for example ? std::map<std::string, aClass*> someMap; vs std::map<int, aClass*> someMap; ...

std::list threading push_back, front, pop_front

Is std::list thread safe? I'm assuming its not so I added my own synchronization mechanisms (I think i have the right term). But I am still running into problems Each function is called by a separate thread. Thread1 can not wait, it has to be as fast as possible std::list<CFoo> g_buffer; bool g_buffer_lock; void thread1( CFoo fram...