I've got a COM object that returns an IEnumUnknown. Is there anything out there that'll turn it into an STL-style iterator? So that I can do something like this:
IEnumUnkPtr pEnumUnk;
// ...something that fills in pEnumUnk...
MagicThing m(pEnumUnk);
std::for_each(m.begin(), m.end(), DoSomethingWithUnk);
...or similar?
...
I am using a set because, i want to use the quick look up property of a sorted container such as a set. I am wondering if I have to use the find member method to get the benefit of a sorted container, or can I also use the static find method in the STL algorithms?
My hunch is that using the static version will use a linear search inste...
Given a sorted vector with a number of values, as in the following example:
std::vector<double> f;
f.pushback(10);
f.pushback(100);
f.pushback(1000);
f.pushback(10000);
I'm looking for the most elegant way to retrieve for any double d the two values that are immediately adjacent to it. For example, given the value "45", I'd like this ...
I would like to do something like this:
container::iterator it = NULL;
switch ( eSomeEnum )
{
case Container1:
it = vecContainer1.begin();
break;
case Container2:
it = vecContainer2.begin();
break;
...
}
for( ; it != itEnd ; ++it )
{
..
}
But I can't create and initialise an iterator to NULL. Is there some way I can do this? Ide...
Greetings :)
I've been reading up on STL containers in my book on C++, specifically the section on the STL and it's containers. Now I do understand each and every one of them have their own specific properties, and I'm close to memorizing all of them... But what I do not yet grasp is in which scenario each of them is used.
Could a kind...
I've found myself writing
for(int i=0;i<myvec.size();i++)
myvec[i]->DoWhatever(param);
a lot, and I'd like to compress this into a foreach statement, but I'm not sure how to get param in there without going super-verbose. I've also got things like
for(int i=0;i<myvec.size();i++)
if(myvec[i]->IsOK())
myvec[i]->DoWhatever(p...
Say we've got:
struct IsEven {
bool operator() (int i) { return i % 2 == 0; }
};
Then:
vector<int> V; // fill with ints
vector<int>::iterator new_end = remove_if(V.begin(), V.end(), IsEven());
V.erase(new_end, V.end());
works fine (it leaves V with only the odd integers). But it seems that the elements from new_end to V.end() ar...
Hi,
I want to create a really easy to use 2D Grid. Each cell in the grid needs to be able to store a load of data. Ideally I would like to be able to traverse through the grid one cell at a time, as well as obtain the immediate neighbours of any of the grid cells.
My first thought was to store a vector of pointers to a Cell's neighbour...
I'm having difficulty in using std::for_each and other algorithms with a multimap, and want to know if someone could help me developing a functor that could pass the appropriate parameter to the "generic" functions.
My specific problem with map/multimap is that their iterators evaluate to a std::pair instead of the contained value (I me...
how can i replace "\r\n" in an std::string
...
Hi,
I know this questions has come up in various guises before, but this is slightly different.
I have a class which contains a std::map. Although I wish to use the map for other purposes inside the class, externally I want to expose an iterator adapter to just the values inside the map (ie the second item in the std::pair).
For examp...
I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I have:
//foo.h
Class Foo {
template<typename T>
static void RemoveVectorDuplicates(std::vector<T...
std::string provides const char* c_str ( ) const which:
Get C string equivalent
Generates a null-terminated sequence
of characters (c-string) with the same
content as the string object and
returns it as a pointer to an array of
characters.
A terminating null character is
automatically appended.
The returned a...
Does anyone know why std::queue, std::stack, and std::priority_queue don't provide a clear() member function? I have to fake one like this:
std::queue<int> q;
// time passes...
q = std::queue<int>(); // equivalent to clear()
IIRC, clear() is provided by everything that could serve as the underlying container. Is there a good reason...
I have a use case wherein numbers are monotonically increasing in an vector of integers
vec[0] = 2
vec[1] = 5
vec[2] = 8
vec[3] = 10
..
If I am passed number 6, I want to return vec[1], since it lies between vec[1] and vec[2], similarly if passes 9 would have to return vec[2]. My experience with STL is limited , so wanted to check can...
Hi,
I'm porting a medium-sized C++ project from Visual Studio 2005 to MacOS, XCode / GCC 4.0.
One of the differences I have just stumbled across has to do with erasing an element from a map. In Visual Studio I could erase an element specified by an iterator and assign the return value to the iterator to get the position of the next ele...
I want to make a function which moves items from one STL list to another if they match a certain condition.
This code is not the way to do it. The iterator will most likely be invalidated by the erase() function and cause a problem:
for(std::list<MyClass>::iterator it = myList.begin(); it != myList.end(); it++)
{
if(myCondition(*it))...
What are the common misuse of using STL containers with iterators?
...
For instance. I have some structure:
s_Some{
std::string lable;
s_some_junk some_junk;
};
And a vector:
std::vector<s_Some> mSome;
And then I fill this vector with a lot of s_Somes.
I need to find an iterator for a single s_Some in this vector, which has a specific lable. So far I just iterate through all of this junk and mat...
I have a std::map like this:
map<wstring,int> Scores;
It stores names of players and scores. When someone gets a score I would simply do:
Scores[wstrPlayerName]++;
When there is no element in the map with the key wstrPlayerName it will create one, but does it initialize to zero or null before the increment or is it left undefined?
...