CPP_vectorの使い方 - RとLinuxと...

RとLinuxと...


CPP_vectorの使い方

Programming

#include <iostream>
#include <vector>
//#include <list>
using namespace std;
//vector<int> v;
void myadd(std::vector<int> &v, int x){
             //void myadd(int x){
 v.push_back(x);
           //v.push_end(x);
}

int main(){

 vector<int> v;
for(int i = 0; i < 5; i++)
    v.push_back(i);

int x = 6;

vector<int>::iterator pv = v.begin();

while(pv != v.end()){
  cout << *pv++ << ' ';
  if(x < 10){
    //cout << x << " in first loop" << endl;
    myadd(v,x++);
    // myadd(x++);
  }
}
cout << endl;
 
while(pv != v.end()){
  cout << *pv++ << ' ';
  if(x < 10){
    cout << x << " in second loop" << endl;
  }
}
   
return 0;
}

ベクトルを引数として関数に渡す

   #include 
   #include 
   using namespace std;
   void myadd(std::vector <int> &v, int x){
     v.push_back(x);
   }
   int main(){
   }
     vector<int> v;
   for(int i = 0; i < 5; i++)
        v.push_back(i);
    int x = 6;
    vector<int>::iterator pv = v.begin();
    while(pv != v.end()){
      cout << *pv++ << ' ';
      if(x < 10){
        cout << x << "in loop" << endl;
        myadd(v,x++);
      
    }
    cout << endl;
    return 0;
 
Link: Programming(3126d) Programming_C(3577d)
Last-modified: 2007-12-06 (木) 15:04:46 (4127d)