stl

How does shared_ptr<> safely allow casting to bool?

I was looking into how std::tr1::shared_ptr<> provides the ability to cast to bool. I've got caught out in the past when trying to create a smart pointer that can be casted to bool as the trivial solution, ie operator bool() { return m_Ptr!=0; } usually ends up being implicitly castable to the pointer type (presumably by type promo...

What permissions does a file written with fstream have?

Suppose I create a file for writing like this: std::ofstream my_file("filename", std::ios_base::out | std::ios_base::trunc); How are the permissions of this file determined? I've had a program running overnight generating files about once a minute - some are 0644 but others are 0660, and there's nothing in my code that should make it ...

Why is lookup in stl::map slower than in stl::vector in my Application?

I am kind of startled, especially after reading this. I use template <class T> int GetPosition(vector<T> mVec, T element) { return find(mVec.begin(), mVec.end(), element) - mVec.begin(); } and template <class T> int GetPosition(map<T, int> mMap, T element) { return mMap.find(element)->second; } as template functions to ge...

set_union with multiset containers?

What's the return of the algorithm std:set_union when one or both input containers are multisets with duplicated objects? Do dups get lost? Let's suppose for example: multiset<int> ms1; ms1.insert(1); ms1.insert(1); ms1.insert(1); ms1.insert(2); ms1.insert(3); multiset<int> ms2; ms2.insert(1); ms2.insert(1); ms2.insert(2); ms2.insert(...

C++: Difficulty with removing an item from a vector

I'm trying to remove an element from a vector. vector<Foo> vecFoo; Foo f1; Foo f2; Foo f3; vecFoo.push_back(f1); vecFoo.push_back(f2); vecFoo.push_back(f3); Foo* pF1 = &f1; vecFoo.erase(std::remove(vecFoo.begin(), vecFoo.end(), *pF1), vecFoo.end()); That last line produces a huge amount of C2784 errors. What am I doing wrong? (Ye...

struct with list<..> in 2 dim. dynamic array segfaults on delete

Hey there, for a little project i wanted to use a struct with an stl container in it. This thingy is then packet into a dynamic 2 dim. array, but when i try to delete it, it segfaults. Here is the code: struct cell{ list<pair<double, double> > alist; }; int main() { struct cell ** myAr = new cell*[5]; for(int i = 0; i < 5; ...

What's the best way to sum the result of a member function for all elements in a container?

Let's say I have the following object: struct Foo { int size() { return 2; } }; What's the best way (most maintainable, readable, etc.) to get the total size of all objects in a vector<Foo>? I'll post my solution but I'm interested in better ideas. Update: So far we have: std::accumulate and a functor std::accumulate and a la...

STL List copies a struct, but the copied values are offset by two memory addresses.

I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can only assume is the latest version; both Code::Blocks and MinGW were installed this past week). My issue crops up under a particular circumstance, and my attempts to write a simpler script that demonstrates the problem have failed (which implies that there...

What are the shortcomings of std::reverse_iterator?

The documentation for boost's specialized iterator adaptors states that boost::reverse_iterator "Corrects many of the shortcomings of C++98's std::reverse_iterator." What are these shortcomings? I can't seem to find a description of these shortcomings. FOLLOW-UP QUESTION: How does boost::reverse_iterator correct these shortcomings? ...

std::vector<float> and double* - How safe is this?

Is it safe to do this? double darray[10]; vector<float> fvector; fvector.insert(fvector.begin(), darray, darray + 10); // double to float conversion // now work with fvector VS2008 gives me a warning about the double to float conversion. How do I get rid of this warning? I don't think it makes sense to cast darray to float* as that...

string contains valid characters

I am writing a method whose signature is bool isValidString(std::string value) Inside this method I want to search all the characters in value are belongs to a set of characters which is a constant string const std::string ValidCharacters("abcd") To perform this search I take one character from value and search in ValidCharacter...

Unit tests for std::map

Does anyone know where I can find unit tests that will test std::map? The reason I ask is because I have written a class that acts as a replacment for std::map and has virtually all the same functionality, so unit tests for std::map will be suitable for my class, too. Of course, I can write my own, but if someone has already written ex...

no match for 'operator<' when trying to insert to a set (c++)?

I'm using gcc 4.3.3 to try to compile the following code: struct testStruct { int x; int y; bool operator<(testStruct &other) { return x < other.x; } testStruct(int x_, int y_) { x = x_; y = y_; } }; int main() { multiset<testStruct> setti; setti.insert(testStruct(10,10)); return 0; } I get this error...

How do I turn relational comparison of pointers into an error?

We've been bitten by the following bug many times: #include <iostream> #include <vector> #include <algorithm> using namespace std; void print(int* pn) { cout << *pn << " "; } int main() { int* n1 = new int(1); int* n2 = new int(2); int* n3 = new int(3); vector<int*> v; v.push_back(n1); v.push_back(n2); v....

C++: Difficulty with partial application

I'm trying to use partial application of function arguments so I can use STL's find_if. Here is a sample program: (Class header and implementation is merged for brevity.) #include <functional> #include <iostream> #include <vector> #include <algorithm> #include <iostream> using namespace std; struct Odp { int id; Odp(int id) ...

Bin packing implementation in C++ with STL

This is my first time using this site so sorry for any bad formatting or weird formulations, I'll try my best to conform to the rules on this site but I might do some misstakes in the beginning. I'm right now working on an implementation of some different bin packing algorithms in C++ using the STL containers. In the current code I stil...

C++ builtin to swap two numerical values?

Does C++ have a built in such as part of STL to swap two numerical values instead of doing: int tmp = var1; var1 = var2; var2 = tmp; Something like this: std::swapValues(var1, var2); Where swapValues is a template. ...

Does such an optimization exist in g++?

Consider the following code snippet: list<someClass>& method(); .... list<someClass> test = method(); What will the behavior of this be? Will this code: Return a reference to the someClass instance returned by return value optimization from method(), and then perform someClass's copy constructor on the reference? Avoid calling th...

sum of elements in a `std::vector`

What are the good ways of finding the sum of all the elements in a std::vector? Suppose I have a vector std::vector<int> vector with a few elements in it. Now I want to find the sum of all the elements. What are the different ways for the same? ...

Is repeatedly calling size() on a container (during loop) bad?

For efficiency reasons, I always avoid writing loops like this: for(std::size_t i = 0; i < vec.size(); ++i) { ... } where vec is an STL container. Instead, I either do const std::size_t vec_size = vec.size(); for(std::size_t i = 0; i < vec_size; ++i) { ... } or use the container iterators. But how bad is the first solution really...