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...
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 ...
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...
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(...
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...
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; ...
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...
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...
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?
...
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...
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...
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...
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...
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....
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)
...
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...
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.
...
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...
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?
...
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...