stl

What is the best method to compare two vectors of CString

Hi I am trying to find the most efficient, optimized and fastest way to compare to std vectors of CString. the strings in question are case-sensitive. I have tried using the == operator for the vector container but this sometimes return false positives. I mean for instance if one vector contains elements in the order (a,b,c) and the oth...

General use cases for C++ containers

What are the general use cases for the C++ standard library containers? bitset deque list map multimap multiset priority_queue queue set stack vector For example, a map is generally better for a paired search. ...

back_insert_iterator with remove_copy_if

I am trying to use a back_insert_iterator with remove_copy_if using vectors but I have compile errors. Do you know why the code below is wrong? #include <iostream> #include <string> #include <algorithm> #include <cassert> #include <vector> #include <iterator> #include <functional> struct checkRem : std::unary_function<int,bool> { ...

Why does storing references (not pointers) in containers in C++ not work?

In my program I have a STL set. set<string> myStrings; To improve efficiency I changed it to only hold pointers. (I don't need actual string copies to be stored.) set<string*> myStrings; I read that it is a good practice to substitute pointers with references when possible. (Of course only if the actual functionality of a pointer i...

In an STL Map of structs, why does the "[ ]" operator cause the struct's dtor to be invoked 2 extra times?

I've created a simple test case exhibiting a strange behavior I've noticed in a larger code base I'm working on. This test case is below. I'm relying on the STL Map's "[ ]" operator to create a pointer to a struct in a map of such structs. In the test case below, the line... TestStruct *thisTestStruct = &testStructMap["test"]; ...gets...

hash_map: why it defines less, rather than equal_to

C++, using Visual Studio 2010. A question about why a user-defined trait of hash_map actually requires total ordering. I have a simple structure, say FOO, which only has a number of integers. I'd like to use hash_map, which is a hash table whose keys are unordered, to store the structure of FOO. I just need a fast searching of its asso...

STL and UTF-8 file input/output. How to do it?

I use wchar_t for internal strings and UTF-8 for storage in files. I need to use STL to input/output text to screen and also do it by using full Lithuanian charset. It's all fine because I'm not forced to do the same for files, so the following example does the job just fine:#include <io.h> #include <fcntl.h> #include <iostream> _set...

Thread safety for STL queue

I am using a queue to communicate between threads. I have one reader and multiple writer threads. My question is do I need to lock the queue every time when I use push/front/pop from the queue for the reader? Can I do something like the following: //reader threads getLock(); get the number of elements from the queue releaseLock(); int ...

How to turn on STL backward compatability?

I am trying to compile something that uses Google's sparsehash include files. libs/include/google/dense_hash_map:93:60: error: ext/hash_fun.h: No such file or directory ^Cmake: *** [all] Interrupt I know that hash_fun.h exists in /usr/include/c++/4.3/backward/hash_fun.h. I am just not sure how to make google sparse hash use it. Any id...

Curious question : What algorithm does STL set_intersect implement?

I spent a considerable amount of time coding in Baeza-Yates' fast set intersection algorithm for one of my apps. While I did marginally out-do the STL set_intersect, the fact that I required the resultant set to be sorted removed any time I had gained from implementing my own algorithm after I sorted the output. Given that the STL set_in...

Problem with STL map iterator copying

I have an STL map that I want to iterate through, and can't seem to get the code to work. The code is: //PowerupInfo is a struct defined in this class's header file std::map<std::string, PowerupInfo> powerups; ...populate powerups std::map<std::string, PowerupInfo>::iterator iter; for (iter = powerups.begin(); iter != powerups.end(); ...

Does placement new call the constructor if the passed pointer is null?

Hello guys, I tried to converted a vc7.1 project to vs2010 which I got from codeproject.(And here's the link h tt p://www.codeproject.com/KB/cpp/transactions.aspx?fid=11253&df=90&mpp=50&noise=3&sort=Position&view=Expanded&fr=1#xx0xx But after converted and modified its configuration. I find it debug unsuccessfully, it says Unhandled ex...

How to convert vector<unsigned char> to int ?

I have vector<unsigned char> filed with binary data. I need to take, lets say, 2 items from vector(2 bytes) and convert it to integer. How this could be done not in C style? ...

How to (deep)copy a map from a const object

Hello, I have another problem I can't seem to solve..., or find on this site... I have an object (called DataObject) with a map, declared as follows: std::map<size_t, DataElement*> dataElements; Now i have a copy function (used in the copy constructor): void DataObject::copy(DataObject const &other) { //here some code to clean...

Extract a struct member from an array of structs

I have an array of structures that contain multiple variables: struct test_case { const int input1; //... const int output; }; test_case tc[] = { {0, /**/ 1}, // ... {99, /**/ 17} }; int tc_size = sizeof(tc) / sizeof(*tc); and I want to extract a vector of the outputs so I can compare them to another array...

Is there a better alternative to an STL comparator with state?

I have a fairly large, sophisticated algorithm that uses a std::priority_queue. In some cases, I want the queue to be a min-queue, and in others, a max-queue. So I have to provide a comparator type that does one or the other. Since the comparator is a template parameter, it is fundamentally part of the type of the std::priority_queue....

STL iterator: "dereferencing" iterator to a temporary. Is it possible?

Hi there. I'm writing a 3D grid for my scientific software and I need to iterate through the nodes of the grid to get their coordinates. Instead of holding each node object in the container I'd rather like to just calculate the coordinates on the fly while iterating. The problem is that stl::iterator requires to return reference to a va...

How to use a struct in std::map ?

I have a complex struct i want to put as a key of the std::map to make a list of all unique objects fast: union somecomplexstruct { struct { more_structs val1, val2; even_more_structs val3, val4; lots_of_more_structs val5; }; unsigned int DATA[3]; }; typedef map<somecomplexstruct, int, greater<som...

Boost.MultiArray Beginner: How to get a 4D-Array with dynamic inner-array-sizes?

Hello, i want to store some kind of distance-matrix (2D), where each entry has some alternatives (different coordinates). So i want to access the distance for example x=1 with x_alt=3 and y=3 with y_alt=1, looking in a 4-dim multi-array with array[1][3][3][1]. The important thing to notice is the following: the 2 most inner arrays/vect...

Efficient push_back of classes and structs

I've seen my colleague do the second snippet quite often. Why is this? I've tried adding print statements to track the ctors and dtors, but both seem identical. std::vector<ClassTest> vecClass1; ClassTest ct1; ct1.blah = blah // set some stuff ... vecClass1.push_back(ct1); std::vector<ClassTest> vecClass2; ...