stdvector

Does std::vector use the assignment operator of its value type to push_back elements?

If so, why? Why doesn't it use the copy constructor of the value type? I get the following error: /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio n `ClassWithoutAss& ClassWithoutAss::operator=(const ClassWithoutAss&)': /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: instantiate d f...

Pointers to elements of std::vector and std::list

Hi, I'm having a std::vector with elements of some class ClassA. Additionally I want to create an index using a std::map<key,ClassA*> which maps some key value to pointers to elements contained in the vector. Is there any guarantee that these pointers remain valid (and point to the same object) when elements are added at the end of the...

Fastest way to create random vectors for benchmarking.

So, I'm just playing around implementing some sorting algorithms in C++, but I'm finding it irritating to benchmark them at the moment, due to the length of time it takes to not run the algorithm, but to create the input data. I currently test each length of input (1000, 2000, ...) 10 times, to gain a somewhat averaged time. For each of ...

returning a std::string with an vector

hi, I'm trying to get "CMtoaPlugin::listArnoldNodes()" to return an "array" of strings std::vector<std::string> ArnoldNodes = CMtoaPlugin::listArnoldNodes(); std::vector<std::string>::iterator it; for ( it=ArnoldNodes.begin() ; it < ArnoldNodes.end(); it++ ) { printf("initialize shader %s\n", *it); } but this is ...

Problem with clear() in custom vector STL container

Following an example in Accelerated C++, I created a custom STL container, which is a simplified version of std::vector, called Vec. Everything worked fine, until, emboldened by success, I tried to add a Vec::clear() that will clear the vector. Here's the latest class definition (only the relevant parts to this question): template <cl...

STL on custom OS - std::list works, but std::vector doesn't

I'm just playing around with a grub-bootable C++ kernel in visual studio 2010. I've gotten to the point where I have new and delete written and things such as dynamically allocated arrays work. I can use STL lists, for example. I can even sort them, after I wrote a memcpy routine. The problem is when I use the std::vector type. Simp...

I'm still having trouble Passing a reference to a Vector.

I asked a similar question in this posting and learned from the replys but I still can't get it to work. test_vector.h #include <vector> class Node { public: std::vector<Node*>& node_dict; int depth; char* cargo; Node* left; Node* right; Node( int a_depth, std::vector<Node*>& a_dict); ...

std::vector optimization

Assuming a loop that reads a lot of values from an std::vector is a bottleneck in my program, it has been suggested I change void f(std::vector<int> v) { ... while (...) { ... int x = v[i] + v[j] ... } } to void f(std::vector<int> v) { int* p_v = &v[0]; ... while (...) { ...

c+ stl sorted vector inplace union

I'd like an efficient method for doing the inplace union of a sorted vector with another sorted vector. By inplace, I mean that the algorithm shouldn't create a whole new vector or other storage to store the union, even temporarily. Instead, the first vector should simple grow by exactly the number of new elements. Something like: vo...

Prettier syntax for "pointer to last element", std::vector?

I'm wondering if there is prettier syntax for this to get a normal pointer (not an iterator) to the last element in a C++ vector std::vector<int> vec; int* ptrToLastOne = &(*(vec.end() - 1)) ; // the other way I could see was int* ptrToLastOne2 = &vec[ vec.size()-1 ] ; But these are both not very nice looking! ...

Initialisation of static vector

Hi! I wonder if there is the "nicer" way of initialising a static vector than below? class Foo { static std::vector<int> MyVector; Foo() { if (MyVector.empty()) { MyVector.push_back(4); MyVector.push_back(17); MyVector.push_back(20); } } } It's an example code...

boost::python: Python list to std::vector

Finally I'm able to use std::vector in python using the [] operator. The trick is to simple provide a container in the boost C++ wrapper which handles the internal vector stuff: #include <boost/python.hpp> #include <vector> class world { std::vector<double> myvec; void add(double n) { this->myvec.push_back(n); }...

Howto call an unmanaged C++ function with a std::vector as parameter from C#?

Hi! I have a C# front end and a C++ backend for performance reasons. Now I would like to call a C++ function like for example: void findNeighbors(Point p, std::vector<Point> &neighbors, double maxDist); What I'd like to have is a C# wrapper function like: List<Point> FindNeigbors(Point p, double maxDist); I could pass a flat array...

Is accessing an array is faster than accessing vector ?

Possible Duplicates: Using arrays or std::vectors in C++, what's the performance gap? std::vector is so much slower than plain arrays? memory is vector of 1000 elements array[] is an integer array of 1000 elements for (iteration = 0; iteration < numiterations; iteration++) { for (j = 1; j < numints; j++) { memory...

How to convert vector<unsigned char> to int ?

I have vector<unsigned char> filed with binary data. I need to take, lets say, 2 items from vector(2 bytes) and convert it to integer. How this could be done not in C style? ...

Efficient push_back of classes and structs

I've seen my colleague do the second snippet quite often. Why is this? I've tried adding print statements to track the ctors and dtors, but both seem identical. std::vector<ClassTest> vecClass1; ClassTest ct1; ct1.blah = blah // set some stuff ... vecClass1.push_back(ct1); std::vector<ClassTest> vecClass2; ...

size of std::vector with struct elements

Hi everybody, I'm having trouble getting the right size of a vector with struct elements. The element class is defined like this (I didn't omit any detail even though I think the only relevant fact is that it is a class containing an int and two doubles): class Interval { public: Interval(int _i = 0, scalar _l = 0, scalar _r = 0) :...