c++做题中用到的函数(自用)

String:

1)getline():读入输入的整行内容
2)tolower():将大写转为小写

string a;
	cin>>a;
	for (int i=0;i<a.length();++i){
        a[i]=tolower(a[i]);}
	cout<<a;

3)toupper():将小写转为大写
4) getchar():函数的功能是从缓冲区中读取一个字符,注意,是一个字符。当缓冲区中没有字符可以读取时,getchar()就会等待我们输入一个字符,然后把它读走,相反,如果缓冲区中存在字符,getchar()就不等我们输入,直接读取缓冲区中的字符。与此对应的是putchar(),它能够向终端输出一个字符。
以下代码

char c;
	cin>>c;
	c=tolower(c);

等同于

char c=tolower(getchar());
  1. append():

append函数是向string的后面追加字符或字符串。
1).向string的后面加C-string
string s = “hello “; const char *c = “out here “;
s.append©; // 把c类型字符串s连接到当前字符串结尾
s = “hello out here”;
2).向string的后面加C-string的一部分
string s=”hello “;const char *c = “out here “;
s.append(c,3); // 把c类型字符串s的前n个字符连接到当前字符串结尾
s = “hello out”;
3).向string的后面加string
string s1 = “hello “; string s2 = “wide “; string s3 = “world “;
s1.append(s2); s1 += s3; //把字符串s连接到当前字符串的结尾
s1 = “hello wide “; s1 = “hello wide world “;
4).向string的后面加string的一部分
string s1 = “hello “, s2 = “wide world “;
s1.append(s2, 5, 5); 把字符串s2中从5开始的5个字符连接到当前字符串的结尾
s1 = “hello world”;
string str1 = “hello “, str2 = “wide world “;
str1.append(str2.begin()+5, str2.end()); //把s2的迭代器begin()+5和end()之间的部分连接到当前字符串的结尾
str1 = “hello world”;
5).向string后面加多个字符
string s1 = “hello “;
s1.append(4,’!’); //在当前字符串结尾添加4个字符!
s1 = “hello !!!”;

  1. substr(pos,len): 返回一个新构造的串对象,从位置pos开始,截取长度为len而产生的子字符串.

algorithm:

1)count(first,last,value):first是容器的首迭代器,last是容器的末迭代器,value是询问的元素,整个函数返回int型。count函数的功能是:统计容器中等于value元素的个数。
2)count_if(first,last,comp) (在comp为true的情况下计数)或者count_if(first,last,value,comp) (这个是在comp为true的情况下统计容器中等于value的元素):first为首迭代器,last为末迭代器,value为要查询的元素,comp为比较bool函数,为true则计数,函数返回型是int。其实comp比较函数才是整个count_if函数的核心,comp比较函数是程序员自己写的,返回值是一个布尔型。
例子:(计算1到10中的奇数)

bool comp(int num)
{
    return num%2;
}
int main()
{
    vector <int> V;
    for(int i=1;i<=10;i++)
        V.push_back(i);
    cout<<count_if(V.begin(),V.end(),comp)<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值