1,string操作
函数 | 定义 |
---|---|
string s; | 定义一个新的空 string 对象,命名为 s |
string s(cp); | 定义一个新的 string 对象,用 cp 所指向的(以空字符null 结束的)C 风格字符串初始化该对象 |
string s(s2); | 定义一个新的 string 对象,并将它初始化为 s2 的副本 |
is >> s; | 从输入流 is 中读取一个以空白字符分隔的字符串,写入 s |
os << s; | 将 s 写到输出流 os 中 |
getline(is, s) | 从输入流 is 中读取一行字符,写入 s |
s1 + s2 | 把 s1 和 s2 串接起来,产生一个新的 string 对象 |
s1 += s2 | 将 s2 拼接在 s1 后面 |
Relational Operators 关系操作符 | 相等运算(== 和 !=)以及关系运算(<、<=、> 和 >=)都可用于 string 对象的比较,等效于(区分大小写的)字典次序的比较 |
函数 | 定义 |
---|---|
s.insert(pos, n, c) | 在下标为 pos 的元素之前插入 n 个字符 c |
s.insert(pos, s2) | 在下标为 pos 的元素之前插入 string 对象 s2 的副本 |
s.insert(pos, s2, pos2, len) | 在下标为 pos 的元素之前插入 s2 中从下标 pos2 开始的 len 个字符 |
s.insert(pos, cp, len) | 在下标为 pos 的元素之前插入 cp 所指向数组的前len 个字符 |
s.insert(pos, cp) | 在下标为 pos 的元素之前插入 cp 所指向的以空字符结束的字符串副本 |
s.assign(s2) | 用 s2 的副本替换 s |
s.assign(s2, pos2, len) | 用 s2 中从下标 pos2 开始的 len 个字符副本替换 s |
s.assign(cp, len) | 用 cp 所指向数组的前 len 个字符副本替换 s |
s.assign(cp) | 用 cp 所指向的以空字符结束的字符串副本替换 s |
s.erase(pos, len) | 删除从下标 pos 开始的 len 个字符 |
2,子串操作
函数 | 定义 |
---|---|
s.substr(pos,n) | 返回一个 string 类型的字符串,它包含 s 中从下标 pos 开始的 n 个字符 |
s.substr(pos) | 返回一个 string 类型的字符串,它包含从下标 pos 开始到 s 末尾的所有字符 |
s.substr() | 返回 s 的副本 |
s.append(args) | 将 args 串接在 s 后面。返回 s 引用 |
s.replace(pos, len, args) | 删除 s 中从下标 pos 开始的 len 个字符,用 args指定的字符替换之。返回 s 的引用 |
s.replace(b, e, args) | 删除迭代器 b 和 e 标记范围内所有的字符,用 args替换之。返回 s 的引用 |
replace 操作用于删除一段指定范围的字符,然后在删除位置插入一组新字符,等效于调用 erase 和 insert 函数。
3,string 查找操作符
函数 | 定义 |
---|---|
s.find( args) | 在 s 中查找 args 的第一次出现 |
s.rfind( args) | 在 s 中查找 args 的最后一次出现 |
s.find_first_of( args) | 在 s 中查找 args 的任意字符的第一次出现 |
s.find_last_of( args) | 在 s 中查找 args 的任意字符的最后一次出现 |
s.find_first_not_of( args) | 在 s 中查找第一个不属于 args 的字符 |
s.find_last_not_of( args) | 在 s 中查找最后一个不属于 args 的字符 |
args | ------参数定义------ |
c, pos | 在 s 中,从下标 pos 标记的位置开始,查找字符 c。pos 的默认值为 0 |
s2, pos | 在 s 中, 从下标 pos 标记的位置开始, 查找 string 对象 s2。 pos 的默认值为 0 |
cp, pos | 在 s 中,从下标 pos 标记的位置形参,查找指针 cp 所指向的 C 风格的以空字符结束的字符串。pos 的默认值为 0 |
cp,pos, n | 在 s 中,从下标 pos 标记的位置开始,查找指针 cp 所指向数组的前 n 个字符。pos 和 n 都没有默认值 |
4,字符串比较,compare函数
s1.compare (args);
compare 函数返回下面列出的三种可能值之一:
- 正数,此时 s1 大于 args 所代表的 string 对象。
- 负数,此时 s1 小于 args 所代表的 string 对象。
- 0,此时 s1 恰好等于 args 所代表的 string 对象。
函数 | 定义 |
---|---|
s.compare(s2) | 比较 s 和 s2 |
s.compare(pos1, n1, s2) | 让 s 中从 pos 下标位置开始的 n1 个字符与 s2 做比较 |
s.compare(pos1, n1, s2, pos2, n2) | 让 s 中从 pos1 下标位置开始的 n1 个字符与 s2 中从 pos2下标位置开始的 n2 个字符做比较 |
s.compare(cp) | 比较 s 和 cp 所指向的以空字符结束的字符串 |
s.compare(pos1, n1, cp) | 让 s 中从 pos1 下标位置开始的 n1 个字符与 cp 所指向的字符串做比较 |
s.compare(pos1, n1, cp, n2) | 让 s 中从 pos1 下标位置开始的 n1 个字符与 cp 所指向的字符串的前 n2 个字符做比较 |
测试1,精确匹配的查找
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name("AnnaBelle");
string::size_type pos1 = name.find("Anna");
cout<<pos1<<endl; // pos1 == 0
return 0;
}
测试2,大小写会影响查找结果
这段代码使 pos1 的值为 npos ——字符串 Anna 与 anna 不匹配
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name("AnnaBelle");
string::size_type pos1 = name.find("anna");
cout<<pos1<<endl; // pos1 == npos
cout<<string::npos<<endl;
return 0;
}
npos 作为查找失败的标识符,输出结果:
tekken@tekken:~/C++WS$ ./a.out
18446744073709551615
18446744073709551615
测试3,查找任意字符
#include <iostream>
#include <string>
using namespace std;
int main()
{
string numerics("0123456789");
string name("r2d2");
string::size_type pos = name.find_first_of(numerics);
cout << "found number at index: " << pos << " element is " << name[pos] << endl; //pos == 1 name[pos]==2
return 0;
}
测试4,查找字符串中出现的所有数字
#include <iostream>
#include <string>
using namespace std;
int main()
{
string numerics("0123456789");
string name("r2d2");
string::size_type pos = 0;
// each trip reset pos to the next instance in name
while ((pos = name.find_first_of(numerics, pos))!= string::npos)
{
cout << "found number at index: " << pos << " element is " << name[pos] << endl;
++pos; // move to the next character
}
return 0;
}
输出结果:
tekken@tekken:~/C++WS$ ./a.out
found number at index: 1 element is 2
found number at index: 3 element is 2
综合测试:将保存在txt文件中的pcl数据读取出来。
测试代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
using namespace std;
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int main(int argc, char **argv) {
std::cout << "Test PCL !" << std::endl;
pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
fstream myfile;
// 读取文件
string line;
myfile.open("/home/tekken/Desktop/pointCloud.txt");
pcl::PointXYZ point;
if (myfile.is_open())
{
while(getline (myfile,line))
{
//cout << line << '\n';
//string test = "41.133,-0.718,1606.473,188";
vector<string>tokens;
Tokenize(line, tokens, ",");
point.x = std::stof(tokens[0]);
point.y = std::stof(tokens[1]);
point.z = std::stof(tokens[2]);
point_cloud_ptr->points.push_back (point);
}
myfile.close();
}
point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
point_cloud_ptr->height = 1;
pcl::visualization::CloudViewer viewer ("test");
viewer.showCloud(point_cloud_ptr);
while (!viewer.wasStopped())
{
;
};
return 0;
}
pointCloud.txt文件格式如下:
28.367,-0.495,1607.750,188
31.450,-0.549,1607.692,188
34.532,-0.603,1607.629,188
38.053,-0.664,1606.549,188
41.133,-0.718,1606.473,188
44.212,-0.772,1606.392,188
47.292,-0.825,1606.304,188
50.711,-0.885,1607.200,188
......
网友评论