Berikut adalah contoh vector pada C++
//Rikki satria
//1008001010018
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string nama;
vector<string>vnama;
// input nama
do{
cout<<"nama (enter=selesai):";
getline (cin, nama);
if(nama!=""){
vnama.push_back(nama);
}
}while(nama!="");
// telusuri vector vnama degan iterator
vector<string>::iterator it;
for(it=vnama.begin(); it!=vnama.end(); ++it){
cout<<"nama:" <<*it<<endl;
}
vnama.pop_back();
cout<<endl;
for(it=vnama.begin(); it!=vnama.end(); ++it){
cout<<"nama:" <<*it<<endl;
}
return 0;
}
//1008001010018
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string nama;
vector<string>vnama;
// input nama
do{
cout<<"nama (enter=selesai):";
getline (cin, nama);
if(nama!=""){
vnama.push_back(nama);
}
}while(nama!="");
// telusuri vector vnama degan iterator
vector<string>::iterator it;
for(it=vnama.begin(); it!=vnama.end(); ++it){
cout<<"nama:" <<*it<<endl;
}
vnama.pop_back();
cout<<endl;
for(it=vnama.begin(); it!=vnama.end(); ++it){
cout<<"nama:" <<*it<<endl;
}
return 0;
}
simpan dengan nama file vector.cpp
Categories:
blog,
c program,
pemrograman2