c++记录

遇到问题留存:
格式:
for( 元素名变量 : 广义集合) { 循环体 }
a.“元素名变量”可以是引用类型,以便直接修改集合元素的值;
b. “元素名变量”也可以是const类型,避免循环体修改元素的值
c. 其中“广义集合”就是“Range(范围)”,是一些元素组成的一个整体

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string> s = { "hello", "world", "!" };
    for (auto &i:s)
    {
        cout << i << endl;
    }
    return 0;
}

结果:
在这里插入图片描述

#include  <iostream>
#include  <vector>

using namespace std;
int main()
{
    int i = 0;
    string lane_csv_ = "abcdefghijklmnopqrstuvwxyz";
    std::vector<std::string> current_waypoints_(26, lane_csv_);
    for (const auto &el : current_waypoints_)
    {
        cout << "第 "<< i+1 << "个位置是" << el[i] << endl;
        i++;
    }
    return 0;
}

结果为:
在这里插入图片描述

#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    string lane_csv_ = "/wangdake/nihao/a";
    std::vector<std::string> dst_multi_file_path(26, lane_csv_);
    for(auto& el : dst_multi_file_path)
    {
        cout << el << endl;
    }
}

在这里插入图片描述

C++string中find,find_first_of和find_last_of的用法

1. size_t find (const string& str, size_t pos = 0)
str.find(str1)

说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos

find函数:http://www.cplusplus.com/reference/string/string/find/

2. size_t find_first_of (const string& str, size_t pos = 0)

str.find_first_of(str1)

从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos

find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/

3.size_t find_last_of (const string& str, size_t pos = npos)

str.find_last_of(str1)

从npos(默认是字符串最后一个,即从后向前查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的最后一个字符的索引位置;如果没有找到则返回string::npos

find_last_of函数:http://www.cplusplus.com/reference/string/string/find_last_of/

erase 三种用法:

1.erase(pos,n);
删除从下标pos开始的n个字符,比如erase(0,1)就是删除第一个字符
2.erase(position);
删除postion处的一个字符(position是一个string类型的迭代器)
3.erase(first,last)
删除从first到last之间的字符(first和last都是迭代器)

#include<iostream>
#include<string>
using namespace std;

int main(){
    string str = "01234567890123456789";

    str.erase(10);
    cout << str << endl;

    str.erase(6, 4);
    cout << str << endl;
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值