C++之string

目录

1.初始化

2.赋值与访问

3.比较

1.== < > <= >=

2.compare

4.子串

5.交换 

6.特征

7.查找

1.查找子串

2.查找字符

8.替换字串

9.插入字符串

10.与c风格的转换

11.迭代器

12.习题

1.编写程序,删除程序中的BY和by

2.计算一个句子中元音字母出现的个数


1.初始化

string s1="Mystring";

string s2("Mystring");

string s3(8,'x');   //s3="xxxxxxxx"

2.赋值与访问

特殊的,虽然在初始化不能将字符赋给字符串,但是赋值可以。

string s1;
string s2;
string s3;

s1='n';    //允许将单个字符赋值字符串

s2=s1;
s3.assign(s1);

string s4(s1+"alpha");  //链接

//重载+=
s3+="alpha";       
s3.append("alpha");



访问

string s1="my heart burns everytime";
for(int i=0;i<s1.length();i++){
    cout<<s1.at(i);
}
cout<<endl;

cout<<s1<<endl;

at(int)提供越界检查,如果i超出了范围,则会报错out_of_range;

3.比较

1.== < > <= >=

string s1="Mystring";
string s2="Yourstring";

cout<<(s1>s2?"s1>s2":"s2>s1");

2.compare

	string s1 = "Mystring";
	string s2 = "Yourstring";

	cout << s1.compare(0, s2.length(), s2);

	string s3(s1 + "alpha");
	cout << s1.compare(0, s3.length(), s3);

4.子串

string s1 = "Mystring";
	string s2 = s1.substr(0, 2);
	cout << s2;        //s2=My

5.交换 

	string s1 = "Mystring";
	string s2 = "Yourstring";
	swap(s1,s2);
	cout << s1 << endl << s2;

6.特征

一个string类通常具有以下特征:

	string s1 = "Mystring";
	cout << "capacity:" << s1.capacity() << endl << "max_size" << s1.max_size() << endl;
	cout << "size:" << s1.size() << endl << "length:" << s1.length() << endl;
	cout << "isEmpty:" << s1.empty();
capacity:15
max_size9223372036854775807
size:8
length:8
isEmpty:0

其中length和size总是相同的,max_size是一个常量。

7.查找

string 提供的const成员函数find及衍生,来查找字符和子串。

1.查找子串

string s1 = "Mystring";
	size_t pos1 = s1.find("string");
	cout << pos1;  //pos1=2

2.查找字符

string s1 = "Mystring";
	size_t pos1 = s1.find_first_of("sy");
	cout << pos1; //pos1=1;

类似的还有

s1.find_first_not_of(string);
s1.find_last_of(string);

8.替换字串

string s2="i love you";

size_t pos=s2.find(" ");
while(pos!=npos){
    s2.replace(pos,1,".");
    pos=s2.find(" ",pos+1);
}

cout<<s2;  //i.love.you

9.插入字符串

	string s2 = "i love you ";
	string s1= "forever";
	s2.insert(11, s1);
	cout << s2;

insert(size_t pos,string s )注意插入的是pos的前面。

10.与c风格的转换

	string s1 = "Mystring";
	char* ptr = new char[s1.length()+1];

	s1.copy(ptr, s1.length(),0);
	ptr[s1.length()] = '\0';
	cout << ptr;

	const char* ptr1 = s1.c_str();
	cout << ptr1;

	const char* ptr2 = s1.data();
	cout << ptr2;
	
	delete[]ptr, ptr1, ptr2;

有三种方法,转换。

11.迭代器

	string s1 = "Mystring";
	
	string::iterator ite= s1.begin();
	while (ite != s1.end()) {
		cout << *ite << endl;
		ite++;
	}

ite的作用类似指针但非指针。

12.习题

1.编写程序,删除程序中的BY和by

string deleteS(string s, string r) {
	size_t pos = s.find(r);
	while (pos != string::npos) {
		s.erase(pos, r.length());
		pos = s.find(r, pos + 1);
	}
	return s;
}
int main() {
	string s = "My BY by buy byd";
	s = deleteS(s, "BY");
	s = deleteS(s, "by");
	cout << s;

}

2.计算一个句子中元音字母出现的个数

int countS(string s, string r) {
	int count=0;
	size_t pos = s.find(r);
	while (pos != string::npos) {
		count++;
		pos = s.find(r, pos + 1);
	}
	cout << "元音字母" << r << "出现的次数:" << count << endl;
	return count;
}
int main() {
	string s = "i love the world";
	string r[5] = { "a","e","i","o","u" };
	for (int i = 0; i < 5; i++) {
		countS(s, r[i]);
	}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值