stl

Should I randomly shuffle before inserting into STL set?

I need to insert 10-million strings into a C++ STL set. The strings are sorted. Will I have a pathological problem if I insert the strings in sorted order? Should I randomize first? Or will the G++ STL implementation automatically rebalance for me? ...

referencing a member function with bind1st and mem_fun

I have a C++ class where I'm trying to use std::bind1st to bind a member function to the 'this' parameter. For example: class MyClass { public: void Foo() { using namespace std; // this works fine this->Bar(); // this also works fine mem_fun( &MyClass::Bar )( this ); // this does not ...

Stl methods with cmath functions

I was trying to write an STL method to take the log of a vector: for_each(vec.begin(),vec.end(),log); But I get no matching function for call to ‘for_each(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, <unres...

Priority Queue Not Sorting

Im trying to implement my own Huffman Coding algorithm and the priority queue for the C++ STL does not seem to be working correctly. I am taking characters from a string and inserting them into a priority queue by order of their frequency in the string. The code compiles and runs without error, the only thing is the tree seems to not be ...

How do I create an elegant for_each() in C++ inside a member function where the operating function is another member function in the same class?

Here is what I'm trying to do: //ImageCache.h: #include <map> #include <SDL.h> typedef pair<const wchar_t*, SDL_Surface*> ImageNameAndSurface; class ImageCache { // Functions public: ImageCache(); ~ImageCache(); SDL_Surface* getImage(const wchar_t* imageFilename); // Variables private: map<const ...

Declaring and defining a function object inside a class member function

Hello to everyone, I wonder if and how it is possible to define a function object inside a classes member function to use it directly with, for example, the std::transform function. I know the example is a bit stupid, it's just to show the problem I'm confronted with. File "example.h" class Example { public: //.. constructor and...

What std::move() is?

What it is? What it does? When it should be used? Good links are appreciated. ...

How to speed-up loading of 15M integers from file stream?

Hello, I have an array of precomputed integers, it's fixed size of 15M values. I need to load these values at the program start. Currently it takes up to 2 mins to load, file size is ~130MB. Is it any way to speed-up loading. I'm free to change save process as well. std::array<int, 15000000> keys; std::string config = "config.dat"; /...

map<T,T>::iterator as parameter type

I have a template class with a private map member template <typename T> class MyClass { public: MyClass(){} private: std::map<T,T> myMap; } I would like to create a private method that accepts an iterator to the map void MyFunction(std::map<T,T>::iterator &myIter){....} However, this gets a compile error: identifier 'iterat...

How do you setup istream_iterator to not ignore blank lines

I'm having problems with istream_iterator reading a file because it ignores blank lines, but I need that those blank lines are included as "". How should I modify the program below to get the 5 lines in my vector? #include <sstream> #include <string> #include <iostream> #include <vector> #include <iterator> using namespace std; int ma...

Can we use a user defined class for the key in a STL map?

I need a key in the map, however, I found it should be multiple data. Can I put these data in one user defined class and put the whole class as a key in the map? Will it impact the time efficiency? What other concerns should be applied here? ...

Input_iterator, find_if and modulus

I implemented an iterator, which has Fibonacci numbers as output. In my main() I'd like to find a number which is dividable by 17 (it's 34). Why doesn't work my find_if statement. Thank you! #include <boost/operators.hpp> #include <algorithm> #include <tr1/functional> struct FibIter: boost::input_iterator_helper<FibIter,uns...

How to generate different random set of numbers, every time within the same program/function?

I understand, using srand(time(0)), helps in setting the random seed. However, the following code, stores the same set of numbers for two different lists. Wondering, how do I generate the different set of numbers when the following function gets called more than once. void storeRandomNos(std::list<int>& dataToStore) { int noofElem...

Where is erase_if?

I've got a container and would like to erase elements based on a predicate. Erase_if sounds familiar, but I can't find it in C++. What's the name and where is it defined? I'd like to use it with a lambda in VS10. ...

Storing elements in the list, in the ascending order

Goal is, I've multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way. Some of the ideas that comes to my mind are a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then. b) Store all the elements in a list and sort the li...

Iterator member behavior

I was expecting the second assert in the following to pass. I'm asking for your help. Edit: It didn't work when I had poss everywhere instead of poss_a in some places. #include <vector> #include <cassert> class Sampler { public: std::vector<int*> poss; std::vector<int*>::const_iterator poss_it; Sampler(std::vector<int*> poss_a) : ...

+= on a vector without boost

Is there any way to use the += operator with a vector without using boost or using a derivated class? Eg. somevector += 1, 2, 3, 4, 5, 6, 7; would actually be somevector.push_back(1); somevector.push_back(2); somevector.push_back(3); etc. ...

Vector sum in C++ design strategy

Possible Duplicate: sum of elements in a std::vector I want to sum the items of a std::vector For example std::vector<int > MYvec; /*some push backs*/ int x=sum(MYVec); //it should give sum of all the items in the vector How to write sum function? I have tried this int sum(const std::vector<int> &Vec) { int res...

Use the auto keyword in C++ STL

I have seen code which use vector, vector<int>s; s.push_back(11); s.push_back(22); s.push_back(33); s.push_back(55); for (vector<int>::iterator it = s.begin(); it!=s.end(); it++) { cout << *it << endl; } It is same as for (auto it = s.begin(); it != s.end(); it++) { cout << *it << endl; } How safe is in this case the use of...

Problem with clear() in custom vector STL container

Following an example in Accelerated C++, I created a custom STL container, which is a simplified version of std::vector, called Vec. Everything worked fine, until, emboldened by success, I tried to add a Vec::clear() that will clear the vector. Here's the latest class definition (only the relevant parts to this question): template <cl...