先说第一个容器
vector: 就是第四章说的那货。好像没啥可说的
大概用法格式
程序示例
#include <iostream>
#include <string>
#include <vector>
const int NUM = 5;
int main()
{
using std::vector;
using std::string;
using std::cin;
using std::endl;
using std::cout;
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 >> 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;
}
return 0;
}
vector的可用函数
迭代器
其实就是广义指针 可以指向指针或类似指针操作的对象 嗯。。 这个解释可以有
定义和基本用法
其他用法
模板追加
push_back()类似appends()
指定区间删除
erase() 距离
scores.earse(scores.begin(), scores.begin() + 2); 删除scores的第一个和第二个元素
插入
insert()
将后面两个元素的内容 插到old_v的前面。。 卧槽 还有这样操作的
以上方法程序示例
#include <iostream>
#include <string>
#include <vector>
struct Review {
std::string title;
int rating;
};
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
int main()
{
using std::cout;
using std::vector;
// Review类型模板类 books
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";
// Review类型的迭代器
vector<Review>::iterator pr;
// 通过迭代器来显示元素内容
for (pr = books.begin(); pr != books.end(); pr++)
ShowReview(*pr);
// 复制构造函数
vector <Review> oldlist(books);
if (num > 3)
{
// 删除指定区间元素
books.erase(books.begin() + 1, books.begin() + 3);
cout << "After erasure:\n";
for (pr = books.begin(); pr != books.end(); pr++)
ShowReview(*pr);
// 将指定区间的元素集插入到books的前面
books.insert(books.begin(), oldlist.begin() + 1, oldlist.begin() + 2);
cout << "After insertion:\n";
for (pr = books.begin(); pr != books.end(); pr++)
ShowReview(*pr);
}
// 交换两个vector内容
books.swap(oldlist);
cout << "Swapping oldlist with books:\n";
for (pr = books.begin(); pr != books.end(); pr++)
ShowReview(*pr);
}
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;
}
程序示例
其他STL函数
for_each()
接收3个参数。 第一个,第二个参数表示的是容器区间,第三个表示要操作的函数(就是把第一个到第二个参数的区间的对象逐一传递到第三个参数,当做该函数的参数传递进去)
Random_shuffle()
该函数接收两个参数,作为指定区间。然后随机排列他们
sort()
排序
两个版本
第一个版本 如果是C++的内置类型(或者是标准函数库中的类型 有定义operator<()的)或者自定义了operator<()函数
sort(book.begin(), books.end()) 默认进行升序排列(要求容器支持随机访问)
第二个版本 不是C++的内置类型,或者不想按照升序排列
sort()接收三个参数。第一、二个与前面一样,就是第三个是比较函数。sort()会将每个元素都放到该函数中进行比较。返回true或false
程序代码示例
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct Review{
std::string title;
int rating;
};
bool operator< (const Review & r1, const Review & r2);
bool worseThan(const Review & r1, const Review & r2);
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
int main()
{
using namespace std;
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";
// 通过ShowReview迭代输出books.begin()到books.end()(不包括end)的元素
for_each(books.begin(), books.end(), ShowReview);
// 升序
sort(books.begin(), books.end());
cout << "Sorted by title:\nRating\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
// 将books所有元素丢到worseThan中排序
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:\nRating\tBook\n";
for_each(books.begin(), books.end(), ShowReview);
}
else
cout << "No entries. ";
cout << "Bye.\n";
return 0;
}
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)
{
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;
}
程序执行结果
C++ 的for循环
以前说的for循环
for (double x : prices)
cout << x << std::endl;
容器也可以用这个 类似于for_each()
for (auto x : books) ShowReview(x);
完结