Vectors
-
Hi there I create a vector of size(10) using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie -
Hi there I create a vector of size(10) using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangieis the vector a type of std::vector or is it CArray??? look at this example for std::vector - int main( ) { using namespace std; vector v1; v1.push_back( 10 ); v1.push_back( 20 ); const int &i = v1.at( 0 ); int &j = v1.at( 1 ); cout << "The first element is " << i << endl; cout << "The second element is " << j << endl; } notice that the item is retrieved with at() function and not with the operator [] ....... do the same for CArray using GetAt() method instead of at() hope this helps Yaron Ask not what your application can do for you, Ask what you can do for your application
-
Hi there I create a vector of size(10) using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangieWith
std::vector< double > v(10);
you create a vector of 10 elements with the value of 0.0 Later, withv.push_back()
you add elements at the end of the vector. When you display the first 10 items of the vector, you only show the zeros inserted at construction. When you let your loop run tov.size()
, (makei
anunsigned int
, or better still,std::vector< double >::size_type
) it should display all you did put into the vector. Hope this did clear things up for you
Who is 'General Failure'? And why is he reading my harddisk?!?
-
Hi there I create a vector of size(10) using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie -
Hi there I create a vector of size(10) using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie -
Just as an excercise - Here is how to use iterators for the display:
#include "stdafx.h"
#include "vector"
#include "iostream"
#include "string"int _tmain(int argc, _TCHAR* argv[])
{
std::vector< double > v(10);for(int i = 0; i < 10; ++i) v.push\_back(i + 0.1); //Display std::vector< double >::iterator it = v.begin(); while (it != v.end()) { std::cout << \*it << "\\n"; ++it; } std::string s; std::cin >> s;
}
Who is 'General Failure'? And why is he reading my harddisk?!?