stl

Are STL Map or HashMaps thread safe?

Can I use a map or hashmap in a multithreaded program without needing a lock? i.e. are they thread safe? I'm wanting to potentially add and delete from the map at the same time. There seems to be a lot of conflicting information out there. By the way, I'm using the STL library that comes with GCC under Ubuntu 10.04 EDIT: Just like th...

std::map and -fno-implicit-templates

I am trying to compile with g++ 4.4 and link a simple program that uses the STL. I am trying to do it using the -fno-implicit-templates so all templates must be instantiated explicitly. I don't understand why this code works: #include <map> //template class std::map<char,char>; template class std::_Rb_tree<char, std::pair <char const, ...

API For KMP or Boyer-Moore string pattern matching in C++ / STL?

Wondering, if I want to replace strstr with a better string matching algorithm, like KMP or Boyer Moore, is there one in C++ or do we have to write on our own? Wondering, what is the practical string matching function that everyone uses other than strstr? This is with respect to C++/STL under Unix/Linux platform. ...

Appending to a vector while iterating over it?

I have a vector that I am iterating over. While iterating, I may append new values to the vector. It looks something like: struct Foo { bool condition; }; void AppendToVec(vector<Foo>& v) { ... v.push_back(...); } vector<Foo> vec; ... for (vector<Foo>::size_type i = 0; i < vec.size(); ++i) { if (vec[i].condition) AppendT...

HELP! will this code cause memory fault after C++ object is out of scope? (STL problem)

I just came across a piece of code written by my ex-colleague few years ago. Honestly, I'm not an C++ expert, so I am seeking help. The code looks like this: std::vector<OBJ> objects; void initobjs() { for (int i=0; i<10; i++) { OBJ obj; obj.type=i; obj.len=16; objects.push_back(obj); } } ...

Is it wrong to dereference a pointer to get a reference?

I'd much prefer to use references everywhere but the moment you use an STL container you have to use pointers unless you really want to pass complex types by value. And I feel dirty converting back to a reference, it just seems wrong. Is it? To clarify... MyType *pObj = ... MyType &obj = *pObj; Isn't this 'dirty', since you can (eve...

Visual C++ hash_multimap not finding any results.

I need some help understanding how stdext::hash_multimap's lower_bound, upper_bound and equal_range work (at least the VS2005 version of it). I have the following code (summarized for the question) #include <hash_map> using stdext::hash_multimap; using std::greater; using stdext::hash_compare; using std::pair; using std::cout; typede...

how to get median value from sorted map

I am using a std::map. Sometimes I will do an operation like: finding the median value of all items. e.g if I add 1 "s" 2 "sdf" 3 "sdfb" 4 "njw" 5 "loo" then the median is 3. Is there some solution without iterating over half the items in the map? ...

Atomically std::vector::push_back() and return index

I need to create a function which appends a value to a vector and returns the index of the value that was just appended. Example: int append(std::vector<int>& numbers, int number){ int retval = numbers.size(); // what if some other thread calls push_back(number) in between these calls? numbers.push_back(number); return retval; ...

Vector constructors

vector<int> ivec(4, 3); can anyone pls explain me the above? I'm newbie & try to learn the c++. Thanks ...

Help with explaining profiler results [STL]

Hi guys, I'm profiling a recent program that is dominated with File read. I'm kind of confused on how to interpret the results. If someone could explain to me what these top four functions are, it would help me a lot. Thanks in advance! % cumulative self self total time seconds seconds calls ...

C++ STL Set: Cannot find() last element inserted

I am in the process of writing an application in which I use the Set class in the C++ STL. I've discovered that the call to set->find() always seems to fail when I query for the last element I inserted. However, if I iterate over the set, I am able to see the element I was originally querying for. To try to get a grasp on what is going ...

C++ Templates - Specifying a container type and that containers element type that it holds

I want to be able to create a function where I specify a parameter to have both a templated container and a templated element type for that container. Is this possible? I get "error C2988: unrecongnizable template declaration/definition" among others. Here is the function in question. template<class Iter, class Elem> void readIntoP(...

Diff function on two arrays, in c++/mfc/stl?

Diff function on two arrays (or how to turn Old into New) Example One[]={2,3,4,5,6,7} Two[]={1,2,3,5,5,5,9} Example Result Diff: insert 1 into One[0], One[]={1,2,3,4,5,6,7} Diff: delete 4 from One[3], One[]={1,2,3,5,6,7} Diff: modify 6 into 5 in One[4], One[]={1,2,3,5,5,7} Diff: modify 7 into 5 in One[5], One[]={1,2,3,5,5,5} Diff: app...

Does the "delete" statement doubly free an object?

Hello C++/STL gurus: Does the "delete" statement below "doubly free" an object? (...object_list is a global vector<object*>...) vector< object * >::iterator it, eit, iter; object *p_object; vector< object * > dead_objects; it = object_list_.begin(); eit = object_list_.end(); //---collect pointers of all dead objects to d...

data repository based on std::map and boost::any: conversion errors

Hi, I am trying to implement a handy data repository or knowledge base for a little program of mine. I use a std::map of boost::any's to hold various pieces of information. For debugging and safety purposes, I have an extra safe accessor ''getVal()'' for the data. A snippet says more than a thousand words: EDIT: <<< Old snippet replac...

a data structure between multimap and vector

Possible Duplicate: A std::map that keep track of the order of insertion? I'm looking for a STL container that works like std::multimap but i can access the members in order of insertion like vector. for example: multimap<char,int> mymultimap; multimap<char,int>::iterator it; mymultimap.insert ( pair<char,int>('a',...

Can a STL map be used with keys of varying sizes

Can a STL map be used for keys of varying sizes? I don't have code for this. I'm still trying to figure out if this can be done and hence my question. (I'm the type that can spend too much time on an impossible problem. I'm hoping to learn from your wisdom). I am working on a look up table that essentially has two keys. A numeric type ...

C++/STL - Program crashes when accessing class pointer instance in a std::map

Okay, I have a function which reads a xml file and creates controls using new and stores them in public member variables of a class called Window: std::map<const char*, Button*> Buttons; std::map<const char*, TextBox*> TextBoxes; std::map<const char*, CheckBox*> CheckBoxes; The Button, TextBox and CheckBox classes are homemade wrapper...

Changing a classes member through an iterator

I'm learning C++ and can't get my head around this problem: I have a simple class A class A { private: int ival; float fval; public: A(int i = 0, float f = 0.0) : ival(i), fval(f) { } ~A(){ } void show() const { cout << ival << " : " << fval << "\n"; } void setVal(int i) { ival = i; } ...