找回密码
 立即注册
收起左侧

C++老鸟日记008 stl使用:string、iostream、vector、list、map

4
回复
14596
查看
[复制链接]
累计签到:41 天
连续签到:1 天
来源: 原创 2018-9-4 10:33:07 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 baizy77 于 2020-4-21 09:22 编辑

版权声明
---------------------------------------------------------------------------------------------------------------------
该文章原创于Qter开源社区(www.qter.org
作者: 女儿叫老白 (白振勇)
转载请注明出处!
---------------------------------------------------------------------------------------------------------------------
本套课程属于:《C++跨平台开发干货》系列课程。
-----------------------------------------------------------------------------

引言
-----------------------------------------------------------------------------
       在C++软件开发过程中,我们会用到各式各样的类库,其中,经常用到的就是stl(标准模板库)。它为我们解决了很多问题,今天我们就来看一下,几个常用的模板类的用法。

正文
-----------------------------------------------------------------------------
如果进行服务端编程,stl、poco等库都是不错的选择。我们今天分享一下stl部分类的使用。
---------------------------------
// 字符串类string.
include<string>

一般用到获取字符串的连接,比如:
string str1 =“abc”;
string str2 =“,..def”;
string str =str1+str2;

或者获取字符串内容,比如:
str.c_str()

---------------------------------
// 输入输出流iostream
#include<iostream>
using std::cout;      // 这样写是防止使用 usingnamespace导致命名空间污染,
usingstd::endl;   // 用到啥就写啥。
using std::cin;

// 下面语句将内容输出到流(除非做过重定向,一般是输出到终端)
cout <<”我想说的是:” << str << endl;

// 下面的语句从输入输出流获取一个键盘输入字符。
char ch = ‘\0’;
cin >> ch;

---------------------------------
// 数组 vector
// vector 是模板类
std::vector<int>v1;         // 声明一个数组,它的成员是int类型。
std::vector<double>v2;     // 声明一个数组,它的成员是double类型。

v1.push_back(2);          // 向v1中压入一个数值,push_back()表示压到数组的最后一个
                                   // v1变成: 2
v1.push_back(5);          // 向v1中压入一个数值,push_back()表示压到数组的最后一个
                                   // v1变成: 2, 5

std::vector<int>::iterator iteVec = v1.begin();
v1.insert(iteVec, 3);      // 向v1中压入一个数值,因为iteVec指向数组的开头,所以此处的insert()表示压到数组的最前一个
                                   // v1变成: 3,  2,  5

有两种方式对vector进行遍历,
// 方法1:用下标
for (int i=0;i<v1.size(); i++) {
    cout << v << endl;
}

// 方法2:使用迭代器
vector<int>::iteratorite = v1.begin();
for (; ite !=v1.end(); ite++) {
       cout << *ite << endl;
}

v1.clear(); // 清空数组v1.

// 删除值=2的成员
ite =v1.begin();
for (;ite!=v1.end();) {
       if ((*ite) == 2) {
       v1.erase(ite++); // 该语法可以删除当前成员,并且将迭代器+1
}
else{
ite++;
}
}

---------------------------------
// 列表 list
列表在内存中一般不会连续排列
列表一般通过迭代器方式访问
list<float>lst;
lst.push_back(1.f);
lst.push_back(13.f);

list<float>::iteratoriteLst = lst.begin();
iteLst =find(lst.begin(), lst.end(), 13.f);
if (iteLst !=lst.end()) {   // 找到了
    cout<< “data 13.f founded!” << endl;
} else {
    cout << “cannot find data 13.f”<< endl;
}
列表遍历,如下:
iteLst =lst.begin();
for (; ite !=lst.end(); iteLst++) {
    cout << *iteLst << endl;
}

---------------------------------
// 映射 map
一般我们用到键值对时会用到map,比如通过学号找某个学员的相关信息
class CStudent
{};
假定学号为int类型;
map<int,CStudent> mapID2Student;

CStudent stud1,stud2;
mapID2Student[201]= stud1;     // 学号=201,对应学员stud1;
mapID2Student[202]= stud2;    // 学号=202,对应学员stud2;

// 搜索某个学员
map<int,CStudent>::iterator iteMap = mapID2Student.find(201);// 搜索id=201的学员
if (iteMap != mapID2Student.end()){
       cout << “id=201, student founded”<< endl;
} else {
       cout << “cannot find student whoseid = 201” << endl;
}
回复

使用道具 举报

尚未签到

2020-4-21 08:08:56 显示全部楼层
/ 方法1:用下标
for (int i=0;i<v1.size(); i++) {
    cout << v << endl;//这里是不是打错了      是v[i]吧
}

点评

感谢您的提醒,已更正。  详情 回复 发表于 2020-4-21 09:22
回复 支持 1 反对 0

使用道具 举报

累计签到:50 天
连续签到:1 天
2018-10-27 19:30:20 显示全部楼层
老师好, v1.push_front(3);   向量这个函数没有。   测试时报错。



te =v1.begin();
for (;ite!=v1.end();) {
       if ((*ite) == 2) {
       v1.erase(ite++); // 该语法可以删除当前成员,并且将迭代器+1
}
else{
ite++
}
}  这个地方删除,又继续迭代会报错。   



teLst =lst.find(13.f);    这个函数也没有,编译不过。  


请老师帮忙解答, 谢谢老师提供这么好的知识内容。  

点评

感谢您的提醒。 第一个错误更新如下: ------------------------------------------------------------------ std::vector::iterator iteVec = v1.begin(); v1.insert(iteVec, 3); // 向v1中压入一个数值,因  详情 回复 发表于 2018-10-27 20:13
回复 支持 反对

使用道具 举报

累计签到:41 天
连续签到:1 天
2018-10-27 20:13:05 显示全部楼层
tan 发表于 2018-10-27 19:30
老师好, v1.push_front(3);   向量这个函数没有。   测试时报错。

感谢您的提醒。

第一个错误更新如下:
------------------------------------------------------------------
std::vector<int>::iterator iteVec = v1.begin();
v1.insert(iteVec, 3);      // 向v1中压入一个数值,因为iteVec指向数组的开头,所以此处的insert()表示压到数组的最前一个
                                   // v1变成: 3,  2,  5
                                                                  
                                                                  
第二个错误更新如下:
------------------------------------------------------------------
else{
  ite++;
}

第三个错误更新如下:
------------------------------------------------------------------
iteLst =find(lst.begin(), lst.end(), 13.f);
回复 支持 反对

使用道具 举报

累计签到:41 天
连续签到:1 天
2020-4-21 09:22:33 显示全部楼层
yzh4545 发表于 2020-4-21 08:08
/ 方法1:用下标
for (int i=0;i

感谢您的提醒,已更正。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了