stdvector

C++ Vector

Look this code(and forgive the miss of knowlegde).It outputs errors that I couldnot solve.I need to declare a vector of elements of struct C,but I need the number of elements be i(a input of type int).I also tried others aproachs but in all of them I recieved an error(cannot convert C to int,etc).How can I do this? # include < iostream...

Using a pointer to an object stored in a vector... c++

I have a vector of myObjects in global scope. std::vector<myObject> A method is passed a pointer to one of the elements in the vector. Can this method increment the pointer, to get to the next element, myObject* pmObj; ++pmObj; // the next element ?? or should it be passed an std::Vector<myObject>::iterator and increment that ins...

Vector of pointers template clearing function fails to compile with "undefined reference" message

Hello all, For a program of mine I made a small function to clear the various std::vectors of pointers that I have. template <class S> void clearPtrVector(std::vector<S*> &a,int size) { for(size_t i = 0; i < size; i++) delete a[i]; a.clear(); } I must have done something wrong here though since when calling this fun...

std::vector of functions

Hi all I want a std::vector to contain some functions, and that more functions can be added to it in realtime. All the functions will have a prototype like this: void name(SDL_Event *event); I know how to make an array of functions, but how do I make a std::vector of functions? I've tried this: std::vector<( *)( SDL_Event *)> functio...

vector of vectors, bad alloc [c++]

Hi, I've a class named Contact and I want to build a data structure of pointers to those objects like a matrix of 127 rows and 20 columns. I've tried to use the std::vector class in this way std::vector < std::vector<Contact* > > matrix (127, std::vector < Contact* > (20)); then, having declarated the following in the header std::ve...

Question about storing array in a std::vector in C++

I am unclear about the following. First, this code compiles fine: #include <vector> typedef struct{ int x1,x2,x3,x4; } ints; typedef std::vector<ints> vec; int main(){ vec v; ints a = {0,1,2,3}; v.push_back(a); } The following code is near identical: #include <vector> typedef std::vector<int[4]> vec; int main(){ vec v; i...

c++ std vector - invalidated iterator question

I have a standard vector of pointers. Under what circumstances might an iterator into this vector become invalidated? I have reason to believe that when an object is deleted, any vector iterator referencing it is thereby invalidated. This does not seem correct to me, however. I do believe this would be the standard behavior of containe...

Out of four std::vector objects select the one with the most elements

I have four std::vector containers that all might (or might not) contain elements. I want to determine which of them has the most elements and use it subsequently. I tried to create a std::map with their respective sizes as keys and references to those containers as values. Then I applied std::max on the size() of each vector to figure ...

How stl vector gives random access

Hello Gurus, Yesterday evening I was using std::vector for my work, and this question popped into my mind: how does vector gives random access? I tried to look into code but was unsuccessful. Can anyone give some pointers? Thanks, Arun ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes out of scope? EDIT: I DON'T WANT TO COPY THE DATA (which would be an easy but ineffective solution). Specifically, I'd like to have some...

Converting between C++ std::vector and C array without copying

I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data. Does std::vector provide access to the underlying C array? I am looking for something like this vector<int> v (4,100) int* pv = v.c_array(); EDIT: Also, is it possible to do the converse, i.e. how would I ini...

std::vector::reserve performance penalty

inline void add(const DataStruct& rhs) { using namespace boost::assign; vec.reserve(vec.size() + 3); vec += rhs.a, rhs.b, rhs.c; } The above function was executed for about 17000 times and it performed (as far as I can see. There was some transformation involved) about 2 magnitudes worse with the call to vector::reserve. I ...

Easiest way to make a cyclic iterator?

I have an object that I want to travel in a continuous loop in a game. I have a series of coordinates in a std::vector that I want to use as waypoints. Is there anyway to make an std::vector::iterator cyclic? The best I can come up with is to have two iterators and then whenever the first iterator is exhausted assign to it the value of...

std::vector reserve method fails to allocate enough memory

Hi, I have a buffer class in my C++ application as follows: class Buffer { public: Buffer(size_t res): _rpos(0), _wpos(0) { _storage.reserve(res); } protected: size_t _rpos, _wpos; std::vector<uint8> _storage; } Sometimes using the constructor fails because its unable to allocate the required memory ...

std::vector insert() reallocation

I was looking through the std::vector code and I found something I didn't quite get. When capacity < size() + 1 it needs to reallocate the buffer so it can insert the new element. What it does (as far as I've been able to extract from the code) is: allocate the new buffer copy the prefix of the old buffer (0 - index of insertion) const...

C++: Easiest way to initialize an STL vector with hardcoded elements

I can create an array initialized with elements like this: int a[] = {10, 20, 30}; How do I create an STL vector and initialize it like the above? What is the best way to do so with the minimum typing effort? The best I can do is: std::vector<int> ints; ints.push_back(10); ints.push_back(20); ints.push_back(30); Though I could sh...

Is std::vector copying the objects with a push_back ?

After a lot of investigations with valgrind, I've made the conclusion that std::vector makes a copy of an object you want to push_back. Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?! Thanks ...

push_back to a Vector

I have a weird problem. I have a vector that I would like to push objects on to like so: vector<DEMData>* dems = new vector<DEMData>(); DEMData* demData = new DEMData(); // Build DEMDATA dems->push_back(*demData); There will be a few hundred DEMData objects in the vector. The problem is when this code is finished, all items are equ...

Is the following std::vector code valid?

std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50] = foo; // but I haven't initialized any of them // so am I assigning into uninitialized memory? Is the above code safe? ...

Emacs, C++ code completion for vectors

Hi, I am new to Emacs, and I have the following code as a sample. I have installed GNU Emacs 23.1.1 (i386-mingw-nt6.1.7600), installed cedet-1.0pre7.tar.gz. , installed ELPA, and company. You can find my simple Emacs configuration at the bottom. The problem is, when I type q[0] in main() and press . (dot), I see the 37 members of the ...