1.String概念
string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。
string和char*的区别:
string是一个类, char*是一个指向字符的指针。
string封装了char*,管理这个字符串,是一个char*型的容器。也就是说string是一个容器,里面元素的数据类型是char*。string不用考虑内存释放和越界。
string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。string提供了一系列的字符串操作函数 查找find,拷贝copy,删除erase,替换replace,插入insert
2.初始化–构造函数
- 默认构造函数 :
string();//构造一个空的字符串string s1
。 - 拷贝构造函数:
string(const string &str);//构造一个与str一样的string。如string s1(s2)
。 - 带参数的构造函数 :
string(const char *s); //用字符串s初始化
string(int n,char c); //用n个字符c初始化
。
#include <iostream>
#include <string>
#include <algorithm>using namespace std;
// string的构造
void func1()
{string s1 = "hello";string s2("world");string s3 = s2; // 拷贝构造string s4(s1); // 拷贝构造string s5(10,'a'); // 字符串长度为10,每个值都是'a'cout << s1 << endl;cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cout << s5 << endl;
}int main()
{func1();system("pause");return 0;
}
3.存取字符
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()。
string的长度
int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
bool empty() const; //当前字符串是否为空
string遍历
string的遍历可以分为数组方式和使用迭代器两种方式。
// 遍历
void func2()
{string str = "hello world";// 1、用数组下标的方式for (unsigned int i = 0; i < str.length(); i++){cout << str[i] << " ";}cout << endl;// 2、通过迭代器string::iterator it;for (it = str.begin(); it != str.end(); it++){cout << *it << " ";}cout << endl;// at(index) 函数for (unsigned int i = 0; i < str.length(); i++){cout << str.at(i) << " ";}cout << endl;// [] 和 at 区别:当数组越界的时候,[] 会直接让程序崩掉、at会抛出异常{try{for (unsigned int i = 0; i < str.length()+10; i++){// cout << str[i] << " ";cout << str.at(i) << " ";}cout << endl;}catch (exception &e){printf ("捕获一个异常: %s\n", e.what());}}}
string赋值操作
string &operator=(const string &s);//把字符串s赋给当前的字符串
string &assign(const char *s); //把字符串s赋给当前的字符串
string &assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
string &assign(const string &s); //把字符串s赋给当前字符串
string &assign(int n,char c); //用n个字符c赋给当前字符串
string &assign(const string &s,int start, int n); //把字符串s中从start开始的n个字符赋给当前字符串
4.和char*类型的转换
从string转换到char*的成员函数主要是:
const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
把string拷贝到char*指向的内存空间的成员函数是:
int copy(char *s, int n, int pos=0) const;
把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。
注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。
/ string ---> char*
// char * ---> string
void func4()
{string str = "hello world";// 返回 string 字符串的 char *类型指针const char *ps = str.c_str();printf("str = %s\n", ps);
}
5.比较操作
int compare(const string &s) const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。
6.string的连接和复制
// 连接和复制
void func3()
{string str1 = "hello";string str2 = " world";// 两个字符串的连接string s3 = "1234 " + str1 + str2 + " abc";str1 += " 1234";cout << str1 << endl;cout << s3 << endl;// 复制string s4;s4 = str1; // 重载 = 运算符cout << s4 << endl;// 对数组进行 复制 string ---> char []char c[100] = {0};str1.copy(c, 5);cout << c << endl;
}
7.查找和替换
// 查找和替换
void func5()
{string str = "123 hello 456 hello 789 hello abc hello";// 查找子串// 找到的是第一个相匹配的字符串的下标int index = str.find("ahello", 0);if (index != string::npos)cout << index << endl;index = str.find("hello", 0);while (index != string::npos){cout << index << endl;index = str.find("hello", index+1);}// 替换string str2 = "123 abc asdasdsa";str2.replace(1, 2, "xxx");cout << str2 << endl;index = str.find("hello", 0);while (index != string::npos){str.replace(index, 5, "HELLO");index = str.find("hello", index+1);}cout << str << endl;
}
8.删除和插入
// 删除
void func6()
{string str = "123 hello 456 hello 789 hello abc hello";// 通过迭代器删除某一个元素str.erase(str.begin()+ 8);cout << str << endl;// 1、str.findint index = str.find('b', 0);if (index != string::npos)str.erase(str.begin()+index);cout << str << endl;// 2、通过 查找算法 去 某个字符,算法 操作都是迭代器 // 和 str 内置的 str.find 区分开 find 是算法库提供的函数 ,返回的是一个迭代器,指向找到的元素string::iterator it = find(str.begin(), str.end(), '9');if (it != str.end())str.erase(it);cout << str << endl;str.erase(4, 5); // 从下标为 4 的位置开始,删除5个元素,如果没有第二个参数,删除4之后所有的元素cout << str << endl;// 区间删除string str1 = "123 hello 456 hello 789 hello abc hello";// 删除方式 是 左闭 又开的 [begin, end)str1.erase(str1.begin(), str1.begin()+4);str1.erase(str1.begin(), str1.end());cout << str1 << endl;
}
9.算法相关
使用transform函数将string里面的字符进行大小写的转换(具体的STL的算法另作介绍):
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <string>using namespace std;void play()
{string s1 = "AAAbbb";transform(s1.begin(), s1.end(), s1.begin(), toupper);cout << "s1" << s1 << endl;string s2 = "AAAbbb";transform(s2.begin(), s2.end(), s2.begin(), tolower);cout << "s2:" << s2 << endl;
}int main()
{ play();system("pause");return 1;
}