c_str() << endl; for (size_t i = 0; i < s1.size(); i++) { cout << s1[i] << " "; } cout << endl; // 迭代器 -- 像指针一样的对象 bit::string::iterator it1 = s1.begin(); while (it1 != s1.end()) { (*it1)--; ++it1; } cout << endl; it1 = s1....
#include<iostream>usingnamespacestd;intmain(){stringstr="Hello";cout<<*str.begin();return0; } 输出: H 此示例显示如何使用 begin() 函数获取字符串的第一个字符。 例子2 让我们看看另一个例子。 #include<iostream>usingnamespacestd;intmain(){stringstr ="B language"; *str.begin()='C';cout<...
如果是const修饰的,就不能修改,只能读。 迭代器iterator(begin、end) 迭代器iterator是一个类型,是定义在string类里面的,需要指定类域才能用。end()是最后一个有效字符的下一个位置,即‘\0’。begin()是起始位置。他模拟指针的行为,但他不是指针,因此他也可读可写。 反向迭代器(rbegin、rend) 当我们的对象是...
begin(); 我们首先写个String类名 后面跟上iterator(迭代器) 再后面加上一个it 等于号的右边写上对象的begin() 或者 end() 我们目前将它当作指针来看待 目前这个阶段这样子理解就好 使用方式如下 string s("hello world"); string::iterator it = s.begin(); while (it != s.end()) { cout << ...
str.begin(): W str.end(): t 4. string.find() 定义: c++11 //str为需要查找的字符串,pos为开始查找的位置string (1) size_t find (const string& str, size_t pos = 0) const noexcept; c-string (2) size_t find (const char* s, size_t pos = 0) const; buffer (3) size_t find...
string s="hello world";cout<<s.c_str()<<endl;// "hello world" 1. 2. 这样看起来,c_str有点多此一举了,明明可以直接输出,不用c_str效果也是一致的。 其实不然,通过c_str还是和直接输出有差别的。 c_str的返回值是一个字符串,而<<对于字符串的输出机制是遇到'\0'中止输出。而string类重载的...
for(string::iterator it = s.begin(); it != s.end(); it++) { cout << *it; } //逆向迭代器 for (string::iterator it = s.rbegin(); it != s.rend(); it++) { cout << *it; } //采用auto实现迭代器 for(auto itr : s) ...
for (auto i = s.begin(); i != s.end(); i++){ cout << *i << ","; } cout << endl; 使用基于范围的for循环语句(C++11新特性提供的一种for语法格式): declaration:定义一个变量,它每次的值都是expression中的基础元素 expression:一个已经定义的对象(变量) ...
begin() + end() 大多数使用在需要使用 STL 提供的算法操作 string 时,比如:采用 reverse 逆置 string。 需要注意的以上三种方式除了遍历 string 对象,还可以遍历是修改 string 中的字符。另外这三种方式对于 string 而言,第一种使用最多。 // const对象必须要用const迭代器void test(const std::string& s){...
利用reserve提高插入数据的效率,避免增容带来的开销1string类对象的访问及遍历操作Element access:函数名称功能说明operator[] (重点)返回pos位置的字符,const string类对象只可调用begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器rbegin + rendbegin获取一个字符的迭代器 + end获取...