10.8/9 这两天看的是关于string的一些函数,写在这里:
1. 今天在小程序里用错了的substr()函数;
find_first_of(); //子串中任意一个字符第一次出现的位置
find_first_not_of(); //不是子串中任意一个字符第一次出现的位置
find_last_of(); //顺序从右往左
find_last_not_of();//顺序从右往左
反向搜索:rfind()
string s="hello world, we love the world";
string subStr = "world";
string::size_type pos = s.rfind(subStr);
函数返回值: string::npos 比任意一个正常下标位置都大的一个值,标示没有找到
3. append(), replace();
原来是world,被替换成了everyone;这是它有意思的地方;
1. 今天在小程序里用错了的substr()函数;
string s="hello world";
string s2 = s.substr(6, 5);// 6:子串的起始位置,5:子串的长度;
cout << s2 << endl;
不知道在C++中数字标示的起始位置,其第二个参数就是长度?而迭代器就是[begin,end) 这种区间模式;不知道是不是容器通用支持迭代器,只有vector, deque才支持下标模式,从目前接触到的容器来说;
2. find函数;
string s="hello world";
string subStr = "wor";
string::size_type pos = s.find(subStr);
find_first_of(); //子串中任意一个字符第一次出现的位置
find_first_not_of(); //不是子串中任意一个字符第一次出现的位置
find_last_of(); //顺序从右往左
find_last_not_of();//顺序从右往左
反向搜索:rfind()
string s="hello world, we love the world";
string subStr = "world";
string::size_type pos = s.rfind(subStr);
函数返回值: string::npos 比任意一个正常下标位置都大的一个值,标示没有找到
3. append(), replace();
string s="hello world";
string s2 = "everyone";
s.replace(6, 5, s2); // 从下标6开始,删除5个字符,以s2中字符填补;
原来是world,被替换成了everyone;这是它有意思的地方;