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...
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...
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...
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? How would I create and use one in C++?
...
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...
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 ...
namespace A
{
#include <iostream>
};
int main(){
A::std::cout << "\nSample";
return 0;
}
...
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...
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...
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;
...
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...
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?
...
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...
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.
...
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...
Is there a performance penalty for working with a vector from the standard library in C++ instead of arrays 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...
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 ...
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?
...