stl

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

Hi all! The title is the main question. The exact scenario (I am 'using namespace std;'): void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) { list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator); } This is the comparator definition: class Substring { // ... class...

writing list of dynamic array to file in binary form>

Hi , I want to write a structure which has a list of integer id. The list can be of varying length. typedef struct ss_iidx_node { int totalFreq; vector < int > docIDList; }s_iidx_node; Now, I wish to write this structure in a file and read it back. How can I do it? Wrting is done: fwrite(&obj,sizeof(s_iidx_node),1,dat_fd...

Broken std::map visualiser in VS2005

I'm using the Intel compiler and visual studio and I can't seem to debug values that are in maps. I get a quick preview which shows the size of the map but the elements only show up as "(error)", I'll illustrate with a quick example, i've generated a map with a single entry myMapVariable[6]=1; if I mouse over I get this "myMapVariable ...

STL maps with user-defined objects

I'm wondering why I can't use STL maps with user-defined classes. When I compile the code below, I get this cryptic error message. What does it mean? Also, why is it only happening with user-defined types? (primitive types are okay when it is used for the key) C:\MinGW\bin..\lib\gcc\mingw32\3.4.5........\include\c++\3.4.5\bits\stl_f...

"Generic" iterator in c++

I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function "generic", so it can take any kind of inputiterators ? I don't really care if it's iterating a deque,a vector or something else, as long as the iterator is iterating Message's. - is this at all straight forward possible in c++ ? ...

Need help with STL sort algorithm

I'm having some troubles with using the std::sort algorithm here. I was reading that you can just overload the less than operator to sort classes, but I have been getting all sorts of errors. I have also tried using a functor as you can see in the example I made below. I was hoping somebody could see what I'm doing wrong here. #include...

Using Protocol Buffers to send icons/small images

I have a simple question about std::string and google's protocol buffers library. I have defined a message like so: message Source { required string Name = 1; required uint32 Id = 2; optional string ImplementationDLL = 3; optional bytes Icon = 4; } I want to use the Icon field to send an image, it most probably will b...

Slicing a vector

I have a std::vector. I want to create iterators representing a slice of that vector. How do I do it. In psudo C++: class InterestingType; void doSomething(slice& s) { for (slice::iterator i = s.begin(); i != s.end(); ++i) { std::cout << *i << endl; } } int main() { std::vector v(); for (int i= 0; i < 10; ++i) {...

reduce the capacity of an stl vector

Hi, Is there a way to reduce the capacity of a vector ? My code inserts values into a vector (not knowing their number beforehand), and when this finishes, the vectors are used only for read operations. I guess I could create a new vector, do a .reseve() with the size and copy the items, but I don't really like the extra copy operati...

What is the best way to use two keys with a std::map?

I have a std::map that I'm using to store values for x & y coordinates. Currently I'm creating a std::string with the two coordinates (ie "12x45") and using it as a key. This doesn't seem like the best way to do it. My other thoughts were to use an int64 and shove the two int32s into it and use it as a key. Or to use a class with the t...

Why isn't there an operator[] for a std::list?

Can anyone explain why isn't the operator[] implemented for a std::list? I've searched around a bit but haven't found an answer. It wouldn't be too hard to implement or am I missing something? ...

Is there any function object to create objects in STL?

Consider the following class: class Person { public: // I don't want any "char *" to be converted to Person implicitly! explicit Person( const char * name ) : name_(name) {}; private: std::string name_; }; Also consider following array of char* data: char STUDENT_NAMES[][20] = { "Bart", "Liza", "Maggie" }; ...

Capturing a time in milliseconds

The following piece of code is used to print the time in the logs: #define PRINTTIME() struct tm * tmptime; time_t tmpGetTime; time(&tmpGetTime); tmptime = localtime(&tmpGetTime); cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptim...

Am I going to be OK for threading with STL given these conditions?

I have a collection of the form: map<key, list<object> > I only ever insert at the back of the list and sometimes I read from the entire map (but I never write to the map, except at initialization). As I understand it, none of the STL containers are thread safe, but I can only really have a maximum of one thread per key. Am I miss...

c++ std::fstream behaviour on MacOS

On MacOS with gcc4.2 should the following code create a new file if none exists? #include <fstream> void test () { std::fstream file ("myfile.txt", std::ios::in | std::ios::out); } By my logic it should, either open up an existing file for read/writing or create a new empty file for read/writing. But the behaviour I get is that i...

hash_map and map which is faster? less than 10000 items

vs2005 support ::stdext::hash_map ::std::map. however it seems ::stdext::hash_map's insert and remove OP is slower then ::std::map in my test. ( less then 10000 items) Interesting.... Can anyone offored a comparision article about them? ...

STL vector reserve() and copy()

Greetings, I am trying to perform a copy from one vector (vec1) to another vector (vec2) using the following 2 abbreviated lines of code (full test app follows): vec2.reserve( vec1.size() ); copy(vec1.begin(), vec1.end(), vec2.begin()); While the call to vec2 sets the capacity of vector vec2, the copying of data to vec2 seems to not ...

std::endl is of unknown type when overloading operator<<

I overloaded operator << template <Typename T> UIStream& operator<<(const T); UIStream my_stream; my_stream << 10 << " heads"; Works but: my_stream << endl; Gives compilation error: error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion) What is ...

Pointer to Value in a std::map

Hi, I have a std::map that is used by multiple threads to store data. The map is declared like this: std::map<int, Call> calls; From each thread, I have to acquire a mutex lock, obtain a pointer or reference to the object belonging to that thread, then release the mutex lock. I can modify the object after that because each object is ...

How to force std::stringstream operator >> to read an entire string?

How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::strings...