c++ vector实例
#include <iostream> #include <string> #include <vector> #include <iostream> using std::cout; using std::cin; using std::endl; using std::vector; using namespace std; int main() { string s = "hell"; string w = "worl!"; s = s + w; //s +=w; for (int ii = 0; ii != s.size(); ii++) { cout << ii << " " << s[ii]; } cout << endl; string::const_iterator cii; int ii = 0; for (cii = s.begin(); cii != s.end(); cii++) { cout << ii++ << " " << *cii << endl; } vector<double> student_marks; int num_students; cout << "Number of students: " << endl; cin >> num_students; student_marks.resize(num_students); for (vector<double>::size_type i = 0; i < num_students; i++) { cout << "Enter marks for student #" << i + 1 << ": " << endl; cin >> student_marks[i]; } cout << endl; for (vector<double>::iterator it = student_marks.begin(); it != student_marks.end(); it++) { cout << *it << endl; } return 0; }
0 h1 e2 l3 l4 w5 o6 r7 l8 !
0 h
1 e
2 l
3 l
4 w
5 o
6 r
7 l
8 !
Number of students:
3
Enter marks for student #1:
3
Enter marks for student #2:
2
Enter marks for student #3:
6
3
2
6