TreeMind树图在线AI思维导图
当前位置:树图思维导图模板高校与高等教育医学《C++ Primer》知识点总结(三)思维导图

《C++ Primer》知识点总结(三)思维导图

  收藏
  分享
免费下载
免费使用文件
L . 浏览量:172022-11-02 15:59:27
已被使用0次
查看详情《C++ Primer》知识点总结(三)思维导图

《C++ Primer》知识点总结

树图思维导图提供 《C++ Primer》知识点总结(三) 在线思维导图免费制作,点击“编辑”按钮,可对 《C++ Primer》知识点总结(三)  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:de3eeb9f11fa6ac31d801a50b532be6c

思维导图大纲

《C++ Primer》知识点总结(三)思维导图模板大纲

using声明

得到命名空间中的成员 using namespace::name

#include<iostream> #include<string> using std::cin; using std::cout; using std::string;

头文件中不应包含using声明

string

string s1; //默认初始化,s1是一个空字符串 string s2 = s1; //s2是s1的副本 string s3 = "hello"; //s3是该字符串字面值的副本 //等价于 string s33("hello"); string s4(10,'C'); //S4的内容是cccccccccc

//读取未知数量的string对象 string word; while(cin>>word) cout<<word<<endl; //使用getline读取一整行 string line; //每次读入一整行,直至到达文件末尾 while(getline(cin,line)) cout<<line<<endl;

//每次读入一整行,遇到空行直接跳过 while(getline(cin,line)) if(!line.empty()) cout<<line<<endl; //每次读入一整行,输出其中超过80个字符的行 while(getline(cin,line)) if(line.size()>80) cout<<line<<endl; string s = s1 + "," + "world" + '\n';

//处理每个字符 string str("some string"); for(auto c : str) cout<<c<<endl;

字面值和string对象相加

当把string对象和字符字面值及字符串字面值混在一条语句中使用时,必须确保每个加法运算符

(+)的两侧的运算对象至少有一个是string

string s2 = s1 + ","; //正确:把一个string对象和一个字面值相加 string s3 = "hello"+","; //错误:两个运算对象都不是string string s4 = s1 + "," + "word"; //正确:每个加法运算符都有一个运算对象是string //reason: (s1+",")+"word" string s5 = "hello" + "," + s1; //错误:不能把字面值直接相加 //reason: ("hello"+",") + s1

vector

vector表示对象的集合,其中所有对象的类型都相同。集合中的每个对象都有一个与之对应的索引

vector<string> articles = {"a","an","the"}; vector<string> v1{"a","an","the"}; vector<string> v1("a","an","the"); //错误

迭代器

访问string对象的字符或vector对象的元素,除了使用下标运算符外,还有一种更通用的机制——迭代器(iterator)

如果容器为空,则begin和end返回的是同一个迭代器

string s("some string"); for(auto it = s.begin; it != s.end() && !isspace(*it); ++it) *it = toupper(*it);

泛型编程

C++程序员习惯性地使用!=,其原因和他们更愿意使用迭代器而非下标地原因一样:因为这种编程风格在标准库提供的所有容器上都有效

auto beg = text.begin(),end = text.end(); auto mid = text.begin() + (end - beg) / 2; while(mid != end && *mid != sought) { if(sought < *mid) end = mid; else beg = mid + 1; mid = beg + (end - beg) / 2; }

相关思维导图模板

《C++ Primer》知识点总结(五)思维导图

树图思维导图提供 《C++ Primer》知识点总结(五) 在线思维导图免费制作,点击“编辑”按钮,可对 《C++ Primer》知识点总结(五)  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:5725866dfdc79fd02c7c7625ad607eb3

《C++ Primer》知识点总结(四)思维导图

树图思维导图提供 《C++ Primer》知识点总结(四) 在线思维导图免费制作,点击“编辑”按钮,可对 《C++ Primer》知识点总结(四)  进行在线思维导图编辑,本思维导图属于思维导图模板主题,文件编号是:57ac5d02c24062bf3cf3a47efbd20348