-
help with vector..
Code:
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
using std::vector;
int main()
{
vector<int> vi;
for (int i = 0; i < 100; i++)
{
Sleep(5000);
vi.push_back(i);
cout< <<endl; //heres the prob , i want to cout vi[0] then vi[1]
Sleep(5000); //as the for loop assigns values to the vector
}
return 0;
}
-
So you want this?
Code:
std::cout<<vi[i]<<std::endl;
-
>> i want to cout vi[0] then vi[1]
What is the problem? Other than the first time through the loop when vi[1] is invalid, you can do that if you want. It doesn't seem to make a lot of sense, though, maybe you could describe better what you are trying to do.
-
?
Code:
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
int main()
{
vector<int> vi;
for (int i = 0; i < 100; i++)
{
// Sleep(5000);
vi.push_back(i);
}
cout << vi[0] << vi[1];
}
-
> cout< <<endl;
Three < ?
Where's the variable you want to output?
-
thanks for the quick replys ...
i was doing vi[0][i] so it would start from 0 element
the whole idea was i didnt know how to cout in order as the for loop assigns
-
You could also use vi.at(i) instead of vi[i]
The added bonus with at() is that it will range-check for you, in case somewhere along the line your program doesn't manage to fill the element you are trying to retrieve. (it will throw an error - But that is far better than the undefined behaviour you would otherwise get)