std

Stange seg fault when using += with strings.

There must be something obvious I don't realize about C++ with this one. load(string & filename){ string command; char delimiter = '/'; size_t delimiterPos = filename.rfind(delimiter); string directory = string(filename.c_str(),delimiterPos); command = "import path "; //want to add directory to end of command string temp = co...

C++: How come random deletion from a std::vector is faster than a std::list?

How come that random deletion from a std::vector is faster than a std::list? What I'm doing to speed it up is swapping the random element with the last and then deleting the last. I would have thought that the list would be faster since random deletion is what it was built for. for(int i = 500; i < 600; i++){ swap(vector1[i], vector...

Adding and removing items without invalidating iterators

I have an object that has a list of 'observers'. These observers get notified of things, and they might respond to this change by adding or removing themselves or other observers from the object. I want a robust, and not unnecessarily slow, way to support this. class Thing { public: class Observer { public: virtual void o...

Why does std::string.find(text,std::string:npos) not return npos?

I am doing a series of searches in a string, and somewhere along the line one of the strings will be missed, and my set of searches should fail. I had expected that once the position reached std::string::npos it would stay there, but it does not. Passing std::string::npos to std::string.find seems to start the search at the beginning ag...

What is a Map and how would I use one in C++?

What is a Map? How would I create and use one in C++? ...

How to use a single namespace across files?

I have a C++ project (VC++ 2008) that only uses the std namespace in many of the source files, but I can't find the "right" place to put "using namespace std;". If I put it in main.cpp, it doesn't seem to spread to my other source files. I had it working when I put this in a header file, but I've since been told that's bad. If I put it...

A std::map that keep track of the order of insertion?

I currently have a std::map<std::string,int> that stores an integer value to an unique string identifier, and I do look up with the string. It does mostly what I want, except for that it does not keep track of the insertion order. So when I iterate the the map to print out the values, they are sorted according to the string; but I want ...

Can I include iostream header file into custom namespace?

namespace A { #include <iostream> }; int main(){ A::std::cout << "\nSample"; return 0; } ...

[C++ stringstream] why this conversion doesn't work?

Below is my func. I call it with if(try_strtol(v, rhs)) and RHS = "15\t// comment" bool try_strtol(int64_t &v, const string& s) { try { std::stringstream ss(s); if ((ss >> v).fail() || !(ss >> std::ws).eof()) throw std::bad_cast(); return true; } catch(...) { return false; } } It returns false, i except true with v...

How to put std::dec/hex/oct into a look-up array

Hello. I have this generic string to number conversion : enum STRING_BASE : signed int { BINARY = -1, OCTAL = 0, DECIMAL = 1, HEX = 2, }; template <class Class> static bool fromString(Class& t, const std::string& str, STRING_BASE base = DECIMAL) { if (base == BINARY) { t = (std::bitset<(sizeof(unsigned long)*8)>(str...

Is there a difference between std::map<int, int> and std::map<const int, int>?

From what I understand, the key in a value pair in an std::map cannot be changed once inserted. Does this mean that creating a map with the key template argument as const has no effect? std::map<int, int> map1; std::map<const int, int> map2; ...

Is the C++ STL std::set thread-safe?

I've a question about the thread safety of std::set. As far as I know I can iterate over a set and add/erase members and that doesn't invalidate the iterators. But consider following scenario: thread 'A' iterates over a set of shared_ptr<Type> thread 'B' occasionally adds items to this set. I've experienced segfaults as the program...

Overriding std functions

I'd like to override the behavior of an std function, say std::time. Is it possible to call std::time and have it routed through my custom function? ...

Why is it so slow iterating over a big std::list ?

As title suggests, I had problems with a program of mine where I used a std::list as a stack and also to iterate over all elements of the list. The program was taking way too long when the lists became very big. Does anyone have a good explanation for this? Is it some stack/cache behavior? (Solved the problem by changing the lists to s...

Thread safe C++ std::set that supports add, remove and iterators from multple threads

I'm looking for something similar to the CopyOnWriteSet in Java, a set that supports add, remove and some type of iterators from multiple threads. ...

C++ std::system 'system' not a Member of std

I receive an error compiling a C++ program in which of the lines makes a call from "std::system(SomeString)". This program compiled 3 years ago, but when compiling it today, I receive an error that states ‘system’ is not a member of ‘std’. Is there something that I must import to use std::system, has it been abandoned, or has it moved to...

Performance penalty for using C++ vector instead of C array.

Is there a performance penalty for working with a vector from the standard library in C++ instead of arrays in C? ...

Why is 'using namespace std;' considered a bad practice in C++?

Okay, sorry for the simplistic question, but this has been bugging me ever since I finished high school C++ last year. I've been told by others on numerous occasions that my teacher was wrong in saying that we should have "using namespace std;" in our programs, and that std::cout and std::cin are more proper. However, they would always b...

Accessing a nested pair

To take apart a pair, the following can be done boost::bind(&std::pair::second, _1); // returns the value of a pair What about using combinations of different containers, how can a nested pair be accessed? For example when I wanted to partition a vector into items contained in a supplemental map and items that where not contained in ...

How is std::string implemented ?

I am curious to know how std::string is implemented and how does it differ from c string?If the standard does not specify any implementation then any implementation with explanation would be great with how it satisfies the string requirement given by standard? ...