简单地说,
1)vector是数组的一种类表示,它提供了自动内存管理功能,可以动态的改变vector对象的长度,并随着元素的添加与删除而增大和缩小。
2)提供了对元素的随机访问。
3)在尾部添加和删除元素的时间是固定的,但在头部或中间插入和删除元素的复杂度为线性时间。
4)vector还可反转容器,增加了两个类方法:rbegin() 和 rend(),这两个类方法返回的迭代器是类级类型 reverse_iterator.
vector是最简单的序列类型,除非其它的类型的特殊优点能更好的满足程序的要求,否则应该默认使用vector
需头文件:vector
#include <vector>
要创建vector对象,可使用通常的vector<type>来指定类型,另外,vector模板使用动态内存分配,因此可以初始化参数来指定大小数量。
vector<int> ratings(5); //固定大小
int n;
cin >> n;
vector<double> scores(n); // 运行时指定大小
可以使用下标访问:
ratings[0] = 9;
for (int i = 0; i < n; ++i)
cout << scores[i] << endl;
vector的简单应用:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //for_each,sort
struct Review{
std::string title;
int rating;
};
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
bool operator<(const Review& r1, const Review& r2);// sort使用
bool WorseThan(const Review& r1, const Review& r2);
int main()
{
using std::cout;
using std::vector;
vector<Review> books;
Review temp;
while (FillReview(temp))
books.push_back(temp);
int num = books.size();
if (num > 0)
{
cout << "Rating\tBook\n";
for (int i = 0; i < num; ++i)
ShowReview(books[i]);
cout << "Reprising:\n"
<< "Rating\tBook\n";
vector<Review>::iterator pr;
for (pr = books.begin(); pr != books.end(); ++pr) //一般遍历用法
ShowReview(*pr);
cout << "for_each:\n";
for_each(books.begin(), books.end(), ShowReview); //也可用for_each代替循环,可避免显式使用迭代器变量
vector<Review> oldlist(books);
if (num > 3)
{
books.erase(books.begin() + 1, books.begin() + 3); //删除区间[books[1],books[3])
cout << "After erasure:\n";
for (pr = books.begin(); pr != books.end(); ++pr)
ShowReview(*pr);
books.insert(books.begin(), oldlist.begin() + 1, oldlist.begin() + 2); //在books的开始位置前插入oldlist[oldlist[1],oldlist[2])
cout << "After insertion:\n";
for (pr = books.begin(); pr != books.end(); ++pr)
ShowReview(*pr);
}
books.swap(oldlist); //同容器的交换
cout << "Swapping oldlist with books:\n";
cout << "books:\n";
for (pr = books.begin(); pr != books.end(); ++pr)
ShowReview(*pr);
cout << "oldlist:\n";
for (pr = oldlist.begin(); pr != oldlist.end(); ++pr)
ShowReview(*pr);
cout << "Random_shuffle(books):\n";
random_shuffle(books.begin(), books.end()); //随机排列books
for_each(books.begin(), books.end(), ShowReview); //显示books
sort(books.begin(), books.end());//books中的元素如为int等常规定义对象,可不写operator<()函数
cout << "sort(books):\n";
for_each(books.begin(), books.end(), ShowReview);
cout << "sort(books) use WorseThan:\n";
sort(books.begin(), books.end(), WorseThan);//自定义降序,按rating
for_each(books.begin(), books.end(), ShowReview);
}
else
{
cout << "Nothing entered, nothing gained.\n";
}
return 0;
}
bool FillReview(Review & rr)
{
std::cout << "Enter book title (quit to quit): ";
std::getline(std::cin, rr.title);
if (rr.title == "quit")
return false;
std::cout << "Enter book rating: ";
std::cin >> rr.rating;
if (!std::cin)
return false;
while (std::cin.get() != '\n')
continue;
return true;
}
void ShowReview(const Review & rr)
{
std::cout << rr.rating << "\t" << rr.title << std::endl;
}
bool operator<(const Review& r1, const Review& r2) //如果对象不是常规r ,是自定义的,需写该函数
{
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;
}
输出:
Enter book title (quit to quit): 111111111111111
Enter book rating: 4
Enter book title (quit to quit): 222222222222222
Enter book rating: 3
Enter book title (quit to quit): 333333333333333
Enter book rating: 2
Enter book title (quit to quit): 444444444444444
Enter book rating: 1
Enter book title (quit to quit): quit
Rating Book
4 111111111111111
3 222222222222222
2 333333333333333
1 444444444444444
Reprising:
Rating Book
4 111111111111111
3 222222222222222
2 333333333333333
1 444444444444444
for_each:
4 111111111111111
3 222222222222222
2 333333333333333
1 444444444444444
After erasure:
4 111111111111111
1 444444444444444
After insertion:
3 222222222222222
4 111111111111111
1 444444444444444
Swapping oldlist with books:
books:
4 111111111111111
3 222222222222222
2 333333333333333
1 444444444444444
oldlist:
3 222222222222222
4 111111111111111
1 444444444444444
Random_shuffle(books):
4 111111111111111
3 222222222222222
1 444444444444444
2 333333333333333
sort(books):
4 111111111111111
3 222222222222222
2 333333333333333
1 444444444444444
sort(books) use WorseThan:
1 444444444444444
2 333333333333333
3 222222222222222
4 111111111111111
请按任意键继续. . .
基于C++11的for 循环:
#include <iostream>
using namespace std;
int main()
{
double prices[5] = {4.99, 10.99, 6.87, 8.75};
for (double x : prices)
cout << x << std::endl;
return 0;
}
输出:
4.99
10.99
6.87
8.75
0
请按任意键继续. . .