博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus 学习笔记 第十六章 泛型编程 STL模板类
阅读量:4127 次
发布时间:2019-05-25

本文共 5313 字,大约阅读时间需要 17 分钟。

先说第一个容器

vector: 就是第四章说的那货。好像没啥可说的

大概用法格式

程序示例

#include 
#include
#include
const int NUM = 5;int main(){ using std::vector; using std::string; using std::cin; using std::endl; using std::cout; vector
ratings(NUM); vector
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 
#include
#include
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
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
::iterator pr;// 通过迭代器来显示元素内容 for (pr = books.begin(); pr != books.end(); pr++) ShowReview(*pr);// 复制构造函数 vector
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 
#include
#include
#include
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
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);

完结 

转载地址:http://xiepi.baihongyu.com/

你可能感兴趣的文章
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>
zju 1005 zoj 1005
查看>>
zju 1006 zoj 1006
查看>>
【虚拟机】虚拟化架构与系统部署(Windows系统安装)
查看>>
字节跳动安卓开发实习生面试分享
查看>>