vector

Accelerated C++, problem 5-6 (copying values from inside a vector to the front)

Hello, I'm working through the exercises in Accelerated C++ and I'm stuck on question 5-6. Here's the problem description: (somewhat abbreviated, I've removed extraneous info.) 5-6. Write the extract_fails function so that it copies the records for the passing students to the beginning of students, and then uses the resize function to...

C++ vector pointer/reference problem

Please take a look at this example: #include <iostream> #include <vector> #include <string> using namespace std; class mySubContainer { public: string val; }; class myMainContainer { public: mySubContainer sub; }; void doSomethingWith( myMainContainer &container ) { container.sub.val = "I was modified"; } int main( ) { ...

C++ Vector at/[] operator speed

In order to give functions the option to modify the vector I can't do curr = myvec.at( i ); doThis( curr ); doThat( curr ); doStuffWith( curr ); But I have to do: doThis( myvec.at( i ) ); doThat( myvec.at( i ) ); doStuffWith( myvec.at( i ) ); (as the answers of my other question pointed out) I'm going to make a hell lot of calls ...

How to initialise a STL vector/list with a class without invoking the copy constructor

I have a C++ program that uses a std::list containing instances of a class. If I call e.g. myList.push_back(MyClass(variable)); it goes through the process of creating a temporary variable, and then immediately copies it to the vector, and afterwards deletes the temporary variable. This is not nearly as efficient as I want, and sucks whe...

how to cout a vector of structs (that's a class member, using extraction operator)

hi, i'm trying to simply cout the elements of a vector using an overloaded extraction operator. the vector contians Point, which is just a struct containing two doubles. the vector is a private member of a class called Polygon, so heres my Point.h #ifndef POINT_H #define POINT_H #include <iostream> #include <string> #include <sstream> s...

Reading Java Vectors in from XML using Betwixt Fails

So I'm writing a program which has a large number of classes that need to be written out to XML, and then read in from XML later. I found that Betwixt turns the Classes I have into XML, writes it to file, and then reads back in the same stuff to instantiate a class. Awesome. Just what I wanted. I happen to have a class which is Suppo...

multidimensional vector rotation and angle computation -- how?

Input: two multidimensional (for example dim=8) vectors a and b. I need to find out the "directed" angle (0-2*Pi, not 0-Pi) between those vectors a and b. And if they are not parallel I need to rotate vector b in plane a,b by "directed" angle L. If they are parallel, plane does not matter, but angle of rotation is still the same L. For...

Vector [] vs copying

What is faster and/or generally better? vector<myType> myVec; int i; myType current; for( i = 0; i < 1000000; i ++ ) { current = myVec[ i ]; doSomethingWith( current ); doAlotMoreWith( current ); messAroundWith( current ); checkSomeValuesOf( current ); } or vector<myType> myVec; int i; for( i = 0; i < 1000000; i ++ ) { doSome...

Convert a python numpy array to c++ stl vector

Hi folks, I'm looking for a way to read in c++ a text file containing numpy arrays and put the data into vector< vector< ... > > , can anyone help me out please ? Thanks a lot. Archy EDIT: format of the text file [[[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9]] [[10 11] [12 13] [14 15] [16 17] [18 19]] [[20 21] [22 23] [24 25] [26 27] [28 29]] ...

How to change a particular element of a C++ STL vector

vector<int> l; for(int i=1;i<=10;i++){ l.push_back(i); } Now, for example, how do I change the 5th element of the vector to -1? I tried l.assign(4, -1); It is not behaving as expected. None of the other vector methods seem to fit. I have used vector as I need random access functionality in my code (using l.at(i)). ...

how is push_back implemented in STL vector?

I was asked this question in an interview. The points I answered are like this 1) an index pointing to the current position; 2) resize if neccessary. Can anybody elaborate more? ...

will a call to std::vector.clear() also set the std::vector.reserve() to zero?

If I use .reserve(items) on a vector, the vector will allocate enough memory for my guess of the number of items that I'll need. If I later on use .clear(), will that just clear the vector or save my earlier defined reserve? thanks. ...

How do I force my std::map to deallocate memory used?

I'm using a std::map, and I can't seem to free the memory back to the OS. It looks like, int main(){ aMap m; while(keepGoing){ while(fillUpMap){ //populate m } doWhatIwantWithMap(m); m.clear();//doesnt free memory back to OS //flush some buffered values into map for next iteration flushIntoMap(m); ...

How to restrict the range of elements of C++ STL vector?

vector<int> l; for(int i=0;i<10;i++){ l.push_back(i); } I want the vector to only be able to store numbers from a specified range (or set). How can that be done, in general? In particular, I want to restrict the vector to beonly be able to store single digits. So, if I do a l[9]++ (in this case l[9] is 9), it should give me an...

Running for loop depending on size of string vector

I have made a string vector vector<string> actor_; and then added elements in it using push_back. I now want to display all of them, for which I need to run a loop according to the number of elements in the vector. For that, I need to run the following loop: for (int i = 0; i < (int)actor_.size; i++) { } but this returns the follo...

Algorithms for finding closest vector

I have a set of vectors. For a vector in that set I like to find the sub set that is closeest to this vector. What algorithm can do this. ...

Removing elements from C++ std::vector

What is the proper way to remove elements from a C++ vector while iterating through it? I am iterating over an array and want to remove some elements that match a certain condition. I've been told that it's a bad thing to modify it during traversal. I guess I should also mention that this is an array of pointers that I need to free befo...

Is the old vector get cleared? If yes, how and when?

I have the following code: void foo() { vector<double> v(100,1); // line 1 // some code v = vector<double>(200,2); // line 2 // some code } what happened to the vector of size 100 after the second line? Is it gets cleared by itself? If the answer is yes, how and when it is cleared? By the way, is there any o...

Compilation problems with vector<auto_ptr<> >

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector<auto_ptr<A> > as; for (int i = 0; i < 10; i++) { auto_ptr<A> a(new A(i)); as.push_back(a); } for (vector<auto_ptr<A> >::itera...

WPF: Rotate a 2D vector

Hello, I want to rotate a given 2D (!) Vector, is there a WPF built-in function for this? Currently I'm doing it manually: Vector v = new Vector(); v.X = 10; v.Y = 10; Vector v2 = new Vector(); v2.X = v.X * Math.Cos(-90 * 180 / Math.PI) - v.Y * Math.Sin(-90 * 180 / Math.PI); v2.Y = v.Y * Math.C...