vector

When returning a pointer, what to return if it's not found? C++

I'm not sure what to return as a default? myDrugs is a private vector<Drug*> container Drug* DrugDealer::getFirstDrugInSack(DrugType drugtobuy) { for (int i = 0; i < myDrugs.size(); i++) { if (myDrugs[i]->getType() == drugtobuy) return myDrugs[i]; } return 0; // is this right? } So I would call it like: D...

Why is this syntax invalid? vectorPointer->[0]

In C++, why is the following element access in a vector invalid? void foo(std::vector<int>* vecPtr) { int n = vecPtr->size(); // ok int a = vecPtr->[0]; // invalid } Instead, we have to write the more cumbersome (*vecPtr)[0] = 1; I think, the operator[] call should just have the same syntax like a method call, and I hate t...

Why not resize and clear works in GotW 54?

Referring to article Gotw 54 by HerbSutter, he explain about 1.Tthe Right Way To "Shrink-To-Fit" a vector or deque and 2.The Right Way to Completely Clear a vector or deque Can we just use container.resize() and container.clear() for above task or Am I missing something. ...

How do I use a (generic) vector in go?

I am using a Vector type to store arrays of bytes (variable sizes) store := vector.New(200); ... rbuf := make([]byte, size); ... store.Push(rbuf); That all works well, but when I try to retrieve the values, the compiler tells me I need to use type assertions. So I add those in, and try for i := 0; i < store.Len(); i++ { el := sto...

Adding elements to a vector inside a c++ class not being stored

Edit: My debugger was lying to me. This is all irrelevant Howdy all, I had a peek at http://stackoverflow.com/questions/637438/adding-element-to-vector, but it's not helpful for my case. I'm trying to add an element (custom class LatLng) to another object (Cluster) from a third object (ClusterManager). When I pass my LatLng to Clust...

How do I correctly use SDL_FreeSurface when dealing with a vector of surfaces.

I have setup a small shooter game as a tutorial for myself in SDL. I have a struct of a projectile struct projectile { SDL_Surface* surface; int x; int y; }; And I put that into a vector. vector<projectile> shot; projectile one_shot; And when I press space I create a new projectile and add it to the vector and then they...

Initializing a ublas vector from a C array

I am writing a Matlab extension using the C++ ublas library, and I would like to be able to initialize my ublas vectors from the C arrays passed by the Matlab interpeter. How can I initialize the ublas vector from a C array without (for the sake of efficiency) explicitly copying the data. I am looking for something along the following li...

matrix and vector template classes in c++

#include <array> template <typename T> class Vector4<T> { std::array<T, 4> _a; // or 'T _a[4];'? }; template <typename T> class Matrix4<T> { std::array<T, 16> _a; // or 'T _a[16];'? //Vector4<T> row0; // or should i use this instead //Vector4<T> row1; // it makes other code easier but how //Vector4<T> row2; // can i...

c++ problem with polymorphism and vectors of pointers

Consider the following example code: class Foo { }; class Bar : public Foo { }; class FooCollection { protected: vector<shared_ptr<Foo> > d_foos; }; class BarCollection : public FooCollection { public: vector<shared_ptr<Bar> > &getBars() { // return d_foos won't do here... } }; I have a problem like this in ...

C++ as in Java?: vector of vectors with *variable* length of int

My model would best use some v int[30][i][N_i]; structure that is 30 vectors of tuples of ints, where v[0] is a dummy, v[1] are plain ints (N_0 of them), v[2] are pairs of int (N_1 pairs) ... v[29] would be 29-tuples of int (N_29 of them) This is not vector<vector<int>> like in "generic-vector-of-vectors-in-c" Apparently, the out...

[Vector] Iterator and 2d vector

vector< vector<int> >::iterator temp = mincost.end(); vector<int> a = *temp; if ( *temp != *(temp--) ) return 0; mincost is a 2d vector, I want to get the last vector<int> of this vector and last--. I don't really understand about iterator :) . Help me !! :D Thx ^^ ...

Problem retuning a vector from a c++ dll to another c++ exe

Hi, I have a function foo() in dll A.dll, whose definition is as follows vector<CustomObject> foo() { vector<CustomObject> customObjectCollection; //code which populates customObjectCollection goes here return customObjectCollection; } I am referring this method vector foo() of dll A from exe B When i make a call...

Causing push_back in vector<int> to segmentaion fault on what seems to be simple operation

I'm working on a program for Project Euler to add all the digits of 2^1000. So far I've been able to track the program segmentation faults when it reaches around 5 digits and tries to push a one onto the vector at line 61 in the function carry(). #include <iostream> #include <vector> #include <string> using namespace std; class MegaN...

std vector + default allocator + direct array access?

If I create a std::vector with the default allocator like this: vector<int> myVec = vector<int>(); myVec.push_back(3); myVec.push_back(5); myVec.push_back(8); Does the vector then store the actual data internally into an array of int? Is it possible to get a pointer to this array and iterate directly over it using this pointer? ...

How to insert into nested vector without invalidating iterator(s)

I have some boolean expressions to evaluate and process. Maybe this would have all been better with Boost, but I'm still learning STL and didn't go that way. I'm now learning about iterator validation, or INvalidation as the case may be. Is there a way to insert a new element into this nested vector below safely? If you don't want t...

Efficient passing of std::vector

When a C++ function accepts an std::vector argument, the usual pattern is to pass it by const reference, such as: int sum2(const std::vector<int> &v) { int s = 0; for(size_t i = 0; i < v.size(); i++) s += fn(v[i]); return s; } I believe that this code results in double dereferencing when the vector elements are accessed, beca...

Drawing Vector Graphics Faster

In my application I want to draw polygons using Windows Create Graphics method and later edit the polygon by allowing the user to select the points of the polygon and allowing to re-position them. I use moue move event to get the new position of the point to get the new coordinates of the point being moved and use Paint event to re-draw...

How to unit test the sorting of a std::vector

I have never used unit testing before, so I'm giving CxxTest a go. I wrote a test to check if a function correctly sorts a std::vector. First I made sure the test failed when the vector wasn't sorted, and then as a sanity check I tested whether std::sort worked (it did, of course). So far, so good. Then I started writing my own sorting ...

iterator for 2d vector

How to create iterator/s for 2d vector (a vector of vectors)? ...

BlackBerry: creating a dynamic no. of buttons based on the contents of a vector

Hi, based on the contents of a vector(IDs), i'm trying to create the corresponding no. of buttons but I'm having problems doing that. I was wondering if anyone could help? Below is the code that i'm using to try to get that done... ButtonField[] btn = new ButtonField[list.IDs.size()]; for(int i=0; i<list.IDs.size(); i++){ btn[i].s...