当前位置:网站首页>3.1-3.3 读书笔记
3.1-3.3 读书笔记
2022-08-10 06:12:00 【CCSUZB】
3.2.2 string对象上的操作
请说明string类的输入运算符和getline函数分别是如何处理空白字符的。
(1)string:类似 is >> s 的读取,string对象会忽略开头的空白并从第一个真正的字符开始,直到遇见下一空白为止。
(2)getline:类似 getline(is, s) 的读取,string对象会从输入流中读取字符,直到遇见换行符为止。
3.2.3 处理string对象中的字符
下面的程序有何作用?它合法吗?如果不合法?为什么?
string s;
cout << s[0] << endl;
不合法。使用下标访问空字符串是非法的行为。
下面的范围for语句合法吗?如果合法,c的类型是什么?
const string s = “Keep out!”;
for(auto &c : s){ /* … */ }
要根据for循环中的代码来看是否合法,c是string 对象中字符的引用,s是常量。因此如果for循环中的代码重新给c赋值就会非法,如果不改变c的值,那么合法。
3.3 标准库类型vector
下列vector对象的定义有不正确的吗?如果有,请指出来。对于正确的,描述其执行结果;对于不正确的,说明其错误的原因。
vector<vector<int>> ivec; // 在C++11当中合法
vector<string> svec = ivec; // 不合法,类型不一样
vector<string> svec(10, "null"); // 合法
下列的vector对象各包含多少个元素?这些元素的值分别是多少?
vector<int> v1; // size:0, no values.
vector<int> v2(10); // size:10, value:0
vector<int> v3(10, 42); // size:10, value:42
vector<int> v4{
10 }; // size:1, value:10
vector<int> v5{
10, 42 }; // size:2, value:10, 42
vector<string> v6{
10 }; // size:10, value:""
vector<string> v7{
10, "hi" }; // size:10, value:"hi"
3.3.3 其他vector操作
下面的程序合法吗?如果不合法,你准备如何修改? vector ivec; ivec[0] = 42;
不合法。应改为:ivec.push_back(42);
如果想定义一个含有10个元素的vector对象,所有元素的值都是42,请例举三种不同的实现方法,哪种方式更好呢?
如下三种:第一种最好
vector<int> ivec1(10, 42);
vector<int> ivec2{
42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
vector<int> ivec3;
for (int i = 0; i < 10; ++i)
ivec3.push_back(42);
边栏推荐
猜你喜欢
COLMAP+OpenMVS实现物体三维重建mesh模型
裸辞—躺平—刷题—大厂(Android面试的几大技巧)
CuteOneP is a PHP-based OneDrive multi-network disk mount program with member synchronization and other functions
网页安全证书错误但无法安装证书的解决办法
语法基础(判断语句)
虚幻5简单第三人称游戏制作文档
Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
XV6 swtch.S详解
第12章 数据库其它调优策略【2.索引及调优篇】【MySQL高级】
腾讯云宋翔:Kubernetes集群利用率提升实践
随机推荐
神经网络可视化有3D版本了,美到沦陷 已开源
The constraints of the database learning table
mysql之两阶段提交
CAP介绍
vsnprint和snprintf的区别
Quickly grasp game resources in one hour and remote hot update
vscode + ccls环境配置
2022 Henan Mengxin League (fifth) game: University of Information Engineering H - Xiao Ming drinking milk tea
Why do games need hot updates
全网可达,交换机和路由器的配置,vlan
order by injection and limit injection, and wide byte injection
3-6月面经总结,200多页真题笔记和详解(含核心考点及6家大厂)
[网络安全]实操AWVS靶场复现CSRF漏洞
High quality WordPress download station 5 play theme template
order by注入与limit注入,以及宽字节注入
共享静态IP与独享静态ip有何区别
Elementary Structure
1413. Stepwise Summation to Get Minimum Positive Numbers
webSocket教程
OpenGL学习笔记(LearnOpenGL)-第二部分 绘制三角形