遇到问题留存:
格式:
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;
}