模板类vector
入门例子:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
const int NUM = 5;
int main() {
/*1*/
//int n;
//cin >> n;
//vector<double> scores(n);
//int* sc = new int[n];//原来都可以用变量来分配动态内存空间
//for (int i = 0;i < n;i++)
//{
// cin >> scores[i];
// cin >> sc[i];
//}
//for (int i = 0;i < n;i++)
// cout << scores[i] << endl;
/*2*/
//vector<int> ratings(NUM);
//vector<string> titles(NUM);
//cout << "You will do exactly as told.You will enter\n" << NUM << " book titles and your ratings (0-10).\n";
//int i;
//for (i = 0;i < NUM;i++) {
// cout << "Enter title #" << i + 1 << ": ";
// getline(cin, titles[i]);
// cout << "Enter your rating(0-10): ";
// cin >> ratings[i];
// cin.get();
//}
//cout << "Thank you.You entered the following:\n" << "Rating\tBook\n";
//for (i = 0;i < NUM;i++)
// cout << ratings[i] << "\t" << titles[i] << endl;
/*3*/
//char a[3][100];
//for (int i = 0;i < 3;i++)
// cin >> a[i];
//cout << a[0] << "\n" << a[1];
system("pause");
return 0;
}
矢量操作
size(): 返回容器中元素数目
swap(): 交换两个容器的内容
begin(): 返回一个指向容器中第一个元素的迭代器
end(): 返回一个表示超过容器尾的迭代器
push_back(): 将元素添加到矢量末尾
erase(): 删除矢量中给定区间的元素(它接受两个迭代参数,第一个迭代器指向区间的起始处,第二个迭代器位于区间终止处的后一个位置)
insert(): 接受三个迭代器参数,第一个参数指定了新元素的插入位置,第二个和第三个迭代器参数定义了被插入区间,该区间通常是另一个容器对象的一部分。
oldv.insert(oldv.end(), newv.begin()+1, newv.end());
具体实例:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct Review {
string title;
int rating;
};
bool FillReview(Review& rr)
{
cout << "Enter book title(quit to quit): ";
getline(cin, rr.title);
if (rr.title == "quit")
return false;
cout << "Enter book rating: ";
cin >> rr.rating;
if (!cin)//如果上一条输入为a输入不匹配,cin调用返回false
return false;
//get rid of rest of input line
while (cin.get() != '\n')
continue;
return true;
}
void ShowReview(const Review& rr)
{
cout << rr.rating << '\t' << rr.title << endl;
}
void show(vector<Review> books)
{
vector<Review>::iterator pr;
for (pr = books.begin();pr != books.end();pr++)
ShowReview(*pr);
}
int main()
{
vector<Review> books;
Review temp;
while (FillReview(temp))
books.push_back(temp);
int num = books.size();
if (num > 0)
{
cout << "Thank you.You entered the following:\n" << "Rating\tBook\n";
for (int i = 0;i < num;i++)
ShowReview(books[i]);
cout << "Reprising:\n" << "Rating\tBook\n";
vector<Review>::iterator pr;
//1
/*for (pr = books.begin();pr != books.end();pr++)
ShowReview(*pr);*/
//2
//show(books);
//3
for (auto x : books) ShowReview(x);
vector<Review> oldlist(books);//copy constructor used
if (num > 3)
{
//remove 2 items
books.erase(books.begin() + 1, books.begin() + 3);
cout << "After erasure:\n";
/*for (pr = books.begin();pr != books.end();pr++)
ShowReview(*pr);*/
//show(books);
for (auto x : books) ShowReview(x);
//insert 1 item
books.insert(books.begin(), oldlist.begin() + 1, oldlist.begin() + 2);
cout << "After insertion:\n";
/*for (pr = books.begin();pr != books.end();pr++)
ShowReview(*pr);*/
//show(books);
for (auto x : books) ShowReview(x);
}
books.swap(oldlist);
cout << "Swapping oldlist with books:\n";
/*for (pr = books.begin();pr != books.end();pr++)
ShowReview(*pr);*/
//show(books);
for (auto x : books) ShowReview(x);
}
else
{
cout << "Nothing entered, nothing gained.\n";
}
system("pause");
return 0;
}
对矢量可执行的其他操作
for_each(): 接受三个参数,前两个参数指定区间,最后一个参数是函数对象。
Random_shuffle(): 接受两个参数,指定随机打乱区间。
sort(): 接受三个参数,前两个指定区间,最后一个指比较函数,可省略(默认从小到大)
具体实现:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Review {
string title;
int rating;
};
bool operator<(const Review& r1, const Review& r2)
{
if (r1.title < r2.title)
return true;
else if (r1.title == r2.title && r1.rating < r2.rating)
return true;
else
return false;
}
bool worseThan(const Review& r1, const Review& r2)
{
if (r1.rating > r2.rating)
return true;
else
return false;
}
bool FillReview(Review& rr)
{
cout << "Enter book title(quit to quit): ";
getline(cin, rr.title);
if (rr.title == "quit")
return false;
cout << "Enter book rating: ";
cin >> rr.rating;
if (!cin)
return false;
while (cin.get() != '\n')
continue;
return true;
}
void ShowReview(const Review& rr)
{
cout << rr.rating << '\t' << rr.title << endl;
}
int main() {
vector<Review> books;
Review temp;
while (FillReview(temp))
books.push_back(temp);
if (books.size() > 0)
{
cout << "Thank you.You entered the following " << books.size() << " ratings:\n"
<< "Rating\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
//small->big
sort(books.begin(), books.end());
cout << "Sorted by title:\nRating\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
//big->small
sort(books.begin(), books.end(), worseThan);
cout << "Sorted by rating:\nRating\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
random_shuffle(books.begin(), books.end());
cout << "After shuffling:\nRanting\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
}
system("pause");
return 0;
}
基于范围的for循环
//basic
double prices[] = {1.23,2.34,3.45}
for(double x:prices)
cout<<x<<" ";
//improve
for_each(books.begin(),books.end(),ShowReview);
//the same as
for(auto x:books) ShowReview(x);