string类的介绍篇:
string类构造函数的使用string赋值操作string字符串的拼接string查找和替换string字符串比较string字符串存取string插入和删除string子串
可能在我公开博客的时候,很多人会有这样的疑问,"你不是算法工程师吗?为何又搞上了C++呢!"。我给出的回答是"C++也可以搞算法的,而且它执行效率比python,php,go等高级语言快多了;还有就是作为编程人员你只会一门编程语言在编程界根本混不开。哈哈言重了,自己体会!!"。介绍正题之前,我想介绍一下C++和Java、python、C#、php、ruby等高级语言的区别吧。
区别一:
编译型:C,C++全部翻译,再执行;解释型:Java、python、C#、php、ruby等语言是边执行边翻译。
区别二:
静态型:C、C++、Java等都属于静态型语言当编写源程序的时候,出现不符合语法的规范,就会提示错误,在编译时变量的数据类型即可确定。动态型:JavaScript、Python、Ruby等是动态型语言,在编译阶段它不会判断代码是否符合规范,在运行的时候才会去判断。
好了,废话不多说了,咱们开始今天的正题了。今天我主要阐述的是C++标准模板库(STL)中的容器string类。我使用的编译器是VS2015.我会在每个string模块下附上具体的实现代码!!那么为何要建立STL呢?为了提高编程的效率啊,这就类似于C中的函数,定义好之后,很多人就无需重复写,直接调用就好,比如C++中的template等等。
容器介绍
容器是通俗一点就是盛放数据的东西,里面装了很多数据,它对我们数据结构中很多经典的算法进行了实现。
string基本概念
string是C++风格的字符串,而string本质上是一个类。
(1). string和char *的区别?
char *是一个指针string是一个类,类的内部封装了char *,说白了是char *类型的容器
string类构造函数的使用
#include<iostream>
using namespace std;
#include<string>
void test1() {
//sting 类构造方法
// 第一种
string s1;
// 第二种
const char * str = "hello world";
string s2(str);
cout << "s2=" << s2 << endl;
// 第三种
string s3(s2);
cout << "s3=" << s3 << endl;
// 第四种
string s4(5, 'a');
cout << "s4=" << s4 << endl;
}
int main() {
test1();
system("pause");
return 0;
}
string赋值操作
#include<iostream>
using namespace std;
#include<string>
void test() {
// 第一种
string s1;
s1 = "hello world";
cout << "s1=" << s1 << endl;
// 第二种
string s2;
s2 = s1;
cout << "s2=" << s2 << endl;
// 第三种
string s3;
s3 = 'a';
cout << "s3=" << s3 << endl;
// 第四种
string s4;
s4.assign("hello c++");
cout << "s4=" << s4 << endl;
// 第五种
string s5;
s5.assign("hello c++", 5);
cout << "s5=" << s5 << endl;
// 第六个
string s6;
s6.assign(s5);
cout << "s6=" << s6 << endl;
// 第七种
string s7;
s7.assign(10, 'w');
cout << "s7=" << s7 << endl;
}
int main() {
test();
system("pause");
return 0;
}
string字符串的拼接
#include<iostream>
using namespace std;
#include<string>
void test_() {
// 第一种
string s = "我";
s += "爱玩游戏!";
cout << s << endl;
s += ":";
cout << s << endl;
// 第二种
string s2 = "DNF LOL";
s += s2;
cout << s << endl;
string s3 = "I";
s3.append(" Love ");
cout << s3 << endl;
s3.append("game aaa", 4);
cout << s3 << endl;
// 从位置0开始,截取3个
s3.append(s2, 0, 3);
cout << s3 << endl;
}
int main() {
test_();
system("pause");
return 0;
}
string查找和替换
#include<iostream>
using namespace std;
#include<string>
void test00() {
// 1.查找find:从左往右
string s = "abcdefg";
int pos = s.find("de");
cout << "position=" << pos << endl;
//rfind 从右往左查找
pos = s.rfind("de");
cout << "position=" << pos << endl;
// 2.替换,从1号位置起,三个字符替换为1111
s.replace(1, 3, "1111");
cout << "s=" << s << endl;
}
int main() {
test00();
system("pause");
return 0;
}
string字符串比较
#include<iostream>
using namespace std;
#include<string>
void test(){
string str1 = "hello";
string str2 = "hello";
if(str1.compare(str2) == 0){
cout << "str1==str2" << endl;
}else if(str1.compare(str2) > 0){
cout << "str1>str2" << endl;
}else{
cout << "str1<str2" << endl;
}
}
string字符串存取
#include<iostream>
using namespace std;
#include<string>
void test(){
/*字符串的取出*/
// 第一种取法
string str = "hello";
for(int i=0;i<str.size();i++){
cout << str[i] <<" ";
}
cout << endl;
//第二种取法
for(int i=0;i<str.size();i++){
cout << str.at(i) << " ";
}
cout <<endl;
/*字符串的修改*/
// 方法一
str[2] = 'a';
cout <<"str=" << str << endl;
// 方法二
str.at(2) = 'b';
cout <<"str=" << str << endl;
}
string插入和删除
注意:插入和删除从0位置开始
#include<iostream>
using namespace std;
#include<string>
void test(){
string str = "hello";
str.insert(1,"222");
cout << "str=" << str << endl;
str.erase(1,3);
cout << "str=" << str << endl;
}
string 子串
#include<iostream>
using namespace std;
#include<string>
void test(){
string str = "hello world";
string subStr = str.substr(6,5);
cout << "subStr == " << subStr << endl;
}
好了,string容器介绍完了,如果你真的想学会它,记得多练啊!!谢谢!!