• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

question about reading values in a vector in C++

dalearyous

Senior member
basically im trying to write a function to read two consecutive values in a vector (they happen to be distances) and calculate the speed at which object is moving and create a new vector with the speeds obtained. im fairly new to C++ and in no way an expert. i can't figure out how to read the first 2 values...do my calculation then output it another vector and repeat.

anyone provide me with any help?

they have to be vectors...not arrays
 
Your question is incomplete.

Read from what: standard input, a file, a sensor device?

Does the data exist already in some format, or do you get to pick your own?

Is space used an issue, so you'd want to use a binary file instead of text?


 
im reading one dimensional vector which terminates with negative number. all values i want are positive.

if this helps i wrote this function to read a data file and place objects into a vector. it is THIS vector i need to read to find out the speed of objects.
 
So you've already passed a vector to this function, now you want to use the values you've read?

Then you want to "iterate" through the vector. Look up the iterator for vectors in your standard library docs.

(You could also get the size of the vector, then loop and use its index operator)
 
thanks for your advice. i found chapter in this c++ book i have about iterators and read the page you sent me to but to be honest i don't quite understand it. do you know where i could find more code examples? i just don't have enough experience to figure that out without more examples or help

i understand the general idea which is to declare an iterater for the vector and two temporary float variables. Then, using the irerater to point to the first value, make the first temp variable equal to what the iterater points to. advance the interater to the next value using '<iterater name>++' and do it all over again.
 
I've never had a reason to use vectors, so all I can recommend is Google. There should be thousands of examples and dozens of tutorials if you get your search keywords right.
 
If you have your data vector set up properly it should be simple to generate your output.

The first thing you need is a function that takes two values and returns a speed. You can do the rest either by treating your vector as an array or by using iterators. Heres some example code.

Just so you have some more info on the iterator stuff, vectors really only give you two iterators, begin() and rbegin() which point to the first and last element respectively. Since you want to access two adjacent elements at a time you need iterators to data[0] and data[1]. data[0] is the same as data.begin(). data[1] is the same as data.begin()++, which is why the first argument of the for loop increments the second value.

In iterators you cant check relative position, you can only check equality. That's why your end condition checks that your iterator isn't equal to data.end().

Also, notice that I always did ++iterator and never iterator++. My STL book explains it like this.
Note that the preincrement operator (prefix ++) is used here. This is because it might have better performance than the postincerement operator. The latter involves a temporary object because it must return the old position of the iterator. For this reason, it generally is best to prefer ++pos over pos++.
It that didn't make any sense just remember to use the ++iterator style whenever your increment is a self contained statement, ie you don't assign it to anything.
 
Originally posted by: Kyteland
If you have your data vector set up properly it should be simple to generate your output.

The first thing you need is a function that takes two values and returns a speed. You can do the rest either by treating your vector as an array or by using iterators. Heres some example code.

Just so you have some more info on the iterator stuff, vectors really only give you two iterators, begin() and rbegin() which point to the first and last element respectively.

They also give you end() which points to the element just past the end of the vector, and rend() which points to the element just before begining of the vector.

Since you want to access two adjacent elements at a time you need iterators to data[0] and data[1]. data[0] is the same as data.begin(). data[1] is the same as data.begin()++, which is why the first argument of the for loop increments the second value.

In iterators you cant check relative position, you can only check equality. That's why your end condition checks that your iterator isn't equal to data.end().

Not quite true. You can check relative position for iteraterators on vector objects, but not on list objects. For example:

vector<double> vx;
vector<double>::iterator vitr;

list<double> lx;
list<double> litr;

(assume we've added some data to both containers)

//This is legal
for(vitr = vx.begin(); vitr < vx.end(); vitr++)

//So is this
vitr = vx.begin() + 5;

//This is not legal
for(litr = lx.begin(); litr < lx.end(); litr++)

//Neitrher is this
litr = lx.begin() + 5;

It's ussually better to stay away from this stuff though, as it might be convenient at some point to switch the underlying container type down the line somewhere. For example, switching from a vector based queue to a list based queue.

Also, notice that I always did ++iterator and never iterator++. My STL book explains it like this.
Note that the preincrement operator (prefix ++) is used here. This is because it might have better performance than the postincerement operator. The latter involves a temporary object because it must return the old position of the iterator. For this reason, it generally is best to prefer ++pos over pos++.
It that didn't make any sense just remember to use the ++iterator style whenever your increment is a self contained statement, ie you don't assign it to anything.

Interesting - I've never heard that before.

Back to the OP - iterators are similar to pointers for fundamental types. But for vectors, you can also use subscripts similarly to native arrays. So, using Kyteland's code as the base, you could do this:
 
Back
Top