How to update a vector in method
Posted
by
gurpinars
on Stack Overflow
See other posts from Stack Overflow
or by gurpinars
Published on 2013-10-19T15:47:55Z
Indexed on
2013/10/19
15:53 UTC
Read the original article
Hit count: 139
I'm new to C++ and trying to understand vectors. My goal is to update a vector in method:
#include <vector>
#include <iostream>
using namespace std;
void test(vector<int>& array){
for(int i=0;i<10;i++){
array.push_back(i);
}
}
int main(){
// some integer value
vector<int> array(10);
test(array);
for(int i=0;i<array.size();++i)
cout<<array.at(i)<<endl;
cout<<"array size:"<<array.size()<<endl;
return 0;
}
output:
0
0
0
0
0
0
0
0
0
0
0
1
2
3
4
5
6
7
8
9
array size:20
I haven't figure out why 10 zeros add vector at first?
© Stack Overflow or respective owner