stl

I want to convert std::string into a const wchar_t *

Is there any method? My computer is AMD64, ::std::string str; BOOL loadU(const wchar_t* lpszPathName, int flag = 0); when I used: loadU(&str); the VS2005 compiler says: Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *' How can I do it ? ...

Is it safe to assume that STL vector storage is always contiguous?

If you have an STL vector which has been resized, is it safe to take the address of element 0 and assume the rest of the vector will follow in memory? e.g. vector<char> vc(100); // do some stuff with vc vc.resize(200); char* p = &vc[0]; // do stuff with *p ...

stl::multimap - how do i get groups of data ?

Multimap essentially has groups of data sorted by the key. I want a method by which I could access these individual groups and get their aggregate values. For example, in a std::multimap< string, int > I store {"Group1", 1}, {"Group1", 2}, {"Group1", 3}, {"Group2", 10}, {"Group2", 11}, {"Group2", 12} Having stored these values, ...

STL priority queue with duplicate keys - is it possible??

Hi everybody, I need to store my class A objects in some data structure. In addition, i would like them to be automatically sorted according to a key, which is in my case an embedded object of another class B. Thus I decided to use a STL priority queue. However it is possible that the 2 or more objects B to have the same key value. ...

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity. ...

How to downsize std::vector?

Is there a way to resize a std::vector to lower capacity when I no longer need previously reserved space? ...

Is std::string size() a O(1) operation?

Is std::string size() a O(1) operation? The implementation of STL I'm using is the one built into VC++ ...

How can I increase the performance in a map lookup with key type std::string?

I'm using an std::map (VC++ implementation) and it's a little slow for lookups via the map's find method. The key type is an std::string. Can I increase the performance of this std::map lookup via a custom key compare override for the map? For example, maybe std::string < compare doesn't take into consideration a simple string::size()...

Approaching STL algorithms, lambda, local classes and other approaches.

One of the things that seems to be necessary with use of STL is a way to specify local functions. Many of the functions that I would normally provide cannot be created using STL function object creation tools ( eg bind ), I have to hand roll my function object. Since the C++ standard forbids local types to be used as arguments in templa...

How to use sgi hash_table in VS2005?

I wrote a C++ project in VS2005, and used lots of STL container with its plus-in STL. However, I found STL in VS2005 does not have a hash_map in it, I want to use SGI hash_map. How can I change my project to use SGI STL? Thanks for Brian's method, it works! And it's simple. ...

How to use find algorithm with a vector of pointers to objects in c++?

I want to find in a vector of Object pointers for a matching object. Here's a sample code to illustrate my problem: class A { public: A(string a):_a(a) {} bool operator==(const A& p) { return p._a == _a; } private: string _a; }; vector<A*> va; va.push_back(new A("one")); va.push_back(new A("two")); va.push_b...

How do you copy the contents of an array to a std::vector in C++ without looping?

I have an array of values that is passed to my function from a different part of the program that I need to store for later processing. Since I don't know how many times my function will be called before it is time to process the data, I need a dynamic storage structure, so I chose a std::vector. I don't want to have to do the standard...

Can I serialize map of STL in MFC using CArchive?

I need to write the content of a map (key is ID of int, value is of self-defined struct) into a file, and load it from the file later on. Can I do it in MFC with CArchive? Thank you! ...

Is it wrong to use auto_ptr with new char[n]

If I declare a temporary auto deleted character buffer using std::auto_ptr<char> buffer(new char[n]); then the buffer is automatically deleted when the buffer goes out of scope. I would assume that the buffer is deleted using delete. However the buffer was created using new[], and so strictly speaking the buffer should be deleted usi...

Anyone have a good shared memory container for C++?

I've long had a desire for an STLish container that I could place into a shared memory segment or a memory mapped file. I've considered the use of a custom allocator and placement new to place a regular STL container into a shared memory segment. (like this ddj article). The problem is that STL containers will internally have pointers t...

C++ map access discards qualifiers (const)

The following code says that passing the map as const into the operator[] method discards qualifiers: #include <iostream> #include <map> #include <string> using namespace std; class MapWrapper { public: const int &get_value(const int &key) const { return _map[key]; } private: map<int, int> _map; }; int main() { ...

What happens if you call erase() on a map element while iterating from begin to end?

In the following code I loop through a map and test if an element needs to be erased. Is it safe to erase the element and keep iterating or do I need to collect the keys in another container and do a second loop to call the erase()? map<string, SerialdMsg::SerialFunction_t>::iterator pm_it; for (pm_it = port_map.begin(); pm_it != port_...

How can I negate a functor in C++ (STL)?

I have some function to find a value: struct FindPredicate { FindPredicate(const SomeType& t) : _t(t) { } bool operator()(SomeType& t) { return t == _t; } private: const SomeType& _t; }; bool ContainsValue(std::vector<SomeType>& v, SomeType& valueToFind) { return find_if(v.begin(), v.end(), FindPredicate...

Drawbacks to templates and the STL in C++

Are there any drawbacks to using the STL or templates. Are there any situations for which they are inappropriate. ...

Multithread read and write to a ::stl::vector, vector resource hard to release

I am writing code in VS2005 using its STL. I have one UI thread to read a vector, and a work thread to write to a vector. I use ::boost::shared_ptr as vector element. vector<shared_ptr<Class>> vec; but I find, if I manipulate the vec in both thread in the same time(I can guarantee they do not visit the same area, UI Thread always read...