1.C++STL笔记--string

1.C++输入输出

c++保留了c的scanf和printf,增加了额外的函数cin和cout。
int a;
cin >> a;
cout << a;

cin和cout的好处:

  1. 相比于c少些繁琐的格式
  2. 由于其采用输入输出流,所以可以连续的输入和输出。
	int a,b,c,d;
	cin>>a>>b>>c;
	cin>>d;
	cout<<a<<b<<c;
	cin<<d;
  1. 用endl进行换行,换行更直观好看
cout<<a<<endl<<b<<endl<<c;

cin和cout的缺点:

  • cin和count比scanf和printf慢,所以在输入输出个数过多时会遇到超时情况,一般输入输出(>1000)容易超时。



2.STL(standard Template Library)与algorithm头文件

  1. STL是一些“容器”的集合。这些容器有list,vector,set,map。
  2. algorithm是对容器继承的一些算法函数,辅助刷题。
  3. sort函数
    概念:迭代器—初步认识理解为指针
#include<iostream>
	#include<algorithm>
	
	using namespace std;
	
	int main()
	{
		int a[] = { 7,8,2,-1,0,9,5,6,11,-8,-6,4,-2 };
		int lenth;
		lenth = sizeof(a) / sizeof(a[0]); //计算数组长度
		sort(a,a+ lenth); //形参为数组的头和尾
		for (int i = 0; i < lenth; i++)
			cout << a[i] << " ";
	
		return 0;
	}

STL—string

  1. 对c中的char * 进行封装为stl的c++中有的string
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	/*string*/
	string s= "HelloWorld!";
	cout << s << endl;
	system("pause");
	return 0;
}
  1. getline(cin,str);从键盘上获取一行字符串
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s;
	getline(cin, s);
	cout << s;
	system("pause");
	return 0;

}
  1. +=介绍
    字符串可以 += 做字符运算,但注意字符的数字不是其原始意义,而是对应ASCII码的意义。像单纯输出数字则,int a=5; string+=(a+‘0’);
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s;
	getline(cin, s);
	cout << s << endl;
	s += "what?";
	cout << s << endl;
	int a = 5;
	s += (a + '0');
	cout << s << endl;
	system("pause");
	return 0;

}

在这里插入图片描述

  1. 算法insert,erase,suubstr
  • 字符串排序sort(s.begin(),s.end())
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
	string s = "543896219";
	sort(s.begin(), s.end());
	cout << s;
	system("pause");
	return 0;
}

在这里插入图片描述

  • erase函数 :删除字符
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
   string s = "754386219";
   sort(s.begin(), s.end());
   cout << s << endl;
   s.erase(s.begin());//删除第一个
   cout << s << endl;	
   s.erase(--s.end());//删除最后一个
   cout << s << endl;
   system("pause");
   return 0;
}

在这里插入图片描述
-suubstr 取字符串

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
   string s = "754386219";
   sort(s.begin(), s.end());
   cout << s << endl;
   s.erase(s.begin());//删除第一个
   cout << s << endl;	
   s.erase(--s.end());//删除最后一个
   cout << s << endl;
   s = s.substr(1, 3);//第一个参数是索引,第二个是取字符个数(1,-1)索引为1截断到最后
   cout << s << endl;
   system("pause");
   return 0;
}

在这里插入图片描述

  1. 循环方式
  • 几种循环方式
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
	string s = "6532198";
	//常规用法
	for (int i = 0; i < s.length(); i++) cout << s << endl;

	cout << endl;
	//迭代器法
	for(string::iterator it = s.begin(); it != s.end(); it++) cout << *it;
	cout << endl;
	//简单版迭代器
	for (auto it = s.begin(); it != s.end(); it++) cout << *it;
	cout << endl;
	//利用c++11新特性做循环
	for (auto x:s) cout << x;
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值