3营销型网站建设银行营销技巧和营销方法
19.1 string概述
-
1、string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是 用char * 表示的。string 与char * 都可以用来表示字符串,那么二者有什么区别呢。
-
2、string和 char * 的比较
(1)string是一个类, char*是一个指向字符的指针。
(2)string封装了char * ,管理这个字符串,是一个char * 型的容器。
(3)string不用考虑内存释放和越界。
(4)string管理char * 所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。 -
3、string提供了一系列的字符串操作函数(这个等下会详讲)
查找find,拷贝copy,删除erase,替换replace,插入insert
19.2 string构造
(1)默认构造函数:
string(); //构造一个空的字符串string s1。
(2)拷贝构造函数:
string(const string &str); //构造一个与str一样的string。如string s1(s2)。
(3)带参数的构造函数
string(const char *s); // 用字符串s初始化
string(int n,char c); // 用n个字符c初始化
例子:
完整示例代码:
#include <iostream>
#include <string>using namespace std;int main()
{string s1; //无参构造函数string s2("helloworld"); //有参构造函数string s3(10, 'a'); string s4(s2); //拷贝构造函数cout << s1 << endl; //重载了输出运算符 << cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cin >> s1; //重载了输入运算符 >> cout << s1 << endl;s1 += "helloworld"; // 重载了 += 运算符if (s1 == s2) // 重载了 == 运算符{}s1 = s1 + s2; // 重载了 = 运算符return 0;
}
运行结果:
19.3 string使用
(1)string的存取字符操作:[ ] 或者 s.at( )
- string类的字符操作:
const char &operator[] (int n) const; // 常函数(不改变成员变量)
const char &at(int n) const; // 常函数(不改变成员变量)
char &operator[] (int n);
char &at(int n);
- operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
主要区别在于at()在越界时会抛出异常,[ ]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
完整示例代码:
#include <iostream>
#include <exception>using namespace std;int main()
{string s("helloworld");cout << s[1] << endl; //重载了下标运算符s[1] = 'x';cout << s << endl;cout << s.at(1) << endl; //通过成员函数来访问下标为 1 的元素//cout << s[20000] << endl; //越界访问程序异常结束try{cout << s.at(10) << endl; //使用成员函数at(),越界访问会抛出异常}catch (exception &e){cout << e.what() << endl;}return 0;
}
运行结果:
(2)从string取得字符串首地址的操作:s.c_str( )
- const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
完整示例代码:
#include <iostream>
#include <string.h>
#include <string>using namespace std;int main()
{char buf[32] = {0};string s("helloworld");strcpy(buf, s.c_str()); //c_str()返回字符串的首地址cout << buf << endl;return 0;
}
运行结果:
(3)把string拷贝的操作:s.copy( )
把字符串s从第0给下标开始,拷贝5个字符到buf中去。
完整示例代码:
#include <iostream>
#include <string.h>using namespace std;int main()
{char buf[32] = {0};string s("helloworld");s.copy(buf, 5); //第二个参数表示拷贝5个字节,第三个参数是默认参数,拷贝的位置,默认是0cout << buf << endl;memset(buf, 0, sizeof(buf));s.copy(buf, 4, 5); // 拷贝四个字节,从下标为5的字符开始拷贝cout << buf << endl;return 0;
}
运行结果:
(4)string的长度:s.length() 与 s.empty()
完整示例代码:
#include <iostream>using namespace std;int main()
{string s("helloworld");cout << s.length() << endl;if (s.empty()){cout << "长度为空" << endl;}else{cout << "长度不为空" << endl;}return 0;
}
运行结果:
(5)string的赋值:重载的 = 运算符 与 s.assign()
示例代码:
#include <iostream>using namespace std;int main()
{string s1("helloworld");s1 = "hello"; //重载了=运算符cout << s1 << endl;const char *s = "this is test";s1.assign(s); // 把s这个字符串赋值给s1cout << s1 << endl;s1.assign(s, 7); // 把s这个字符串的前7个字符赋值给s1cout << s1 << endl;s1.assign(5, 'a'); //把5个a赋值给s1cout << s1 << endl;string s2("hey boy");s1.assign(s2); //把对象s2赋值给s1cout << s1 << endl;s1.assign(s2, 4, 3); // 把s2从第四个字符开始往后3个字符,赋值给字符串s1cout << s1 << endl;return 0;
}
运行结果:
(6)string字符串连接:重载 += 运算符 与 s.append()
示例代码:
#include <iostream>using namespace std;int main()
{string s1("helloworld");s1 += "1234"; //重载了 += 运算符cout << s1 << endl;string s2("abcdefg");s1 += s2;cout << s1 << endl;const char *s = "haha";s1.append(s);cout << s1 << endl;s1.append(s, 2); // 把 s 前两个字符加在s1后面cout << s1 << endl;s1.append(s2);cout << s1 << endl;s1.append(s2, 4, 2); // 把 s 第四个字符开始往后两个字符加在s1后面cout << s1 << endl;s1.append(10, 'x'); // 把10个x字符追加在字符串cout << s1 << endl;return 0;
}
运行结果:
(7)string的比较:s.compare()
示例代码:
#include <iostream>using namespace std;int main()
{string s1("helloworld");string s2("helloboy");const char *s = "hellogirl";if (s1.compare(s2) > 0){cout << s1 << " > " << s2 << endl;}if (s1.compare(s) > 0){cout << s1 << " > " << s << endl;}return 0;
}
运行结果:
(8)string的子串:s.substr()
示例代码:
#include <iostream>using namespace std;int main()
{string s("helloworld");cout << s.substr() << endl; // 返回完整的字符串scout << s.substr(5, 5) << endl; // 从下标五开始,往后五个字符return 0;
}
运行结果:
(9)string的查找和替换:s.find() 与 s.replace()
-
查找
int find(char c,int pos=0) const;
//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos=0) const;
//从pos开始查找字符串s在当前字符串的位置int find(const string &s, int pos=0) const;
//从pos开始查找字符串s在当前字符串中的位置注意:find函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const;
//从pos开始从后向前查找字符c在当前字符串中的位置
int rfind(const char *s, int pos=npos) const; int rfind(const string &s, int pos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1 -
替换
string &replace(int pos, int n, const char *s);
//删除从pos开始的n个字符,然后在pos处插入串s
string &replace(int pos, int n, const string &s);
//删除从pos开始的n个字符,然后在pos处插入串s
void swap(string &s2);
//交换当前字符串与s2的值
完整示例代码:
#include <iostream>
#include <string.h>using namespace std;int main()
{int p;string s1("helloworld");string s2("world");p = s1.find('o');cout << p << endl; // 4p = s1.find('x'); //不存在返回-1cout << p << endl; // -1p = s1.find('o', 5);cout << p << endl; // 6p = s1.find("ll");cout << p << endl; // 2p = s1.find(s2);cout << p << endl; // 5p = s1.rfind('o'); //反向查找(但是返回的位置还行从左边0开始)cout << p << endl; // 6s1.replace(5, 5, "xxx");cout << s1 << endl; // helloxxxxxs1.replace(5, 3, s2);cout << s1 << endl; // helloworldstring s3("helloworldhelloworldhelloworldhelloworld");p = s3.find("world");while (p != -1){s3.replace(p, strlen("world"), "x");p = s3.find("world", p + strlen("x"));}cout << s3 << endl;return 0;
}
运行结果:
(10)String的区间删除和插入:s.insert() 与 s.erase()
完整示例代码:
#include <iostream>using namespace std;int main()
{string s1("helloworld");string s2("12345");s1.insert(0, "this is ");// 从下标为0处,插入this iscout << s1 << endl; s1.insert(10, s2); // 从下标为10处,插入s2cout << s1 << endl; s1.insert(0, 5, 'x'); // 从下标为0处,插入5个xcout << s1 << endl;s1.erase(0, 20);cout << s1 << endl; // 删除下标0到20return 0;
}
运行结果: