码迷,mamicode.com
首页 > 其他好文 > 详细

string

时间:2017-05-27 22:26:12      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:size   打印   字母   string   logs   比较   out   while   return   

一、包含的头文件

#include <string>
using namespace std;

 二、读写

1、cin,cout

int main()
{
    string s;
    cin>>s;
    cout << s<< endl;
    return 0;
}

 while()

int main()
{
    string s;
    while(cin>>s)
    cout << s<< endl;
    return 0;
}

 2、getline

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    string s;
    while(getline(cin,s,*))
    cout<<s<<endl;
    return 0;
}

三、操作

1、常用的string操作

  s.empty    如果s为空串,则返回true,否则返回false

  s.size     返回s中字符的个数

  s[n]      返回s中的位置为n的字符,位置从0开始计数

  s1 + s2    把s1和s2连接成一个新的字符串,返回新生成的字符串

  s1 = s2    把s1内容替换成s2的副本

  v1 == v2    比较v1和v2的内容,相等返回true,否则返回false

  !=, <, <=    保持这些操作惯有含义

  > 和 >=

2、string::size_type类型

  任何储存strinf的size操作结果的变量必须为string::size_type类型。特别重要的是,不要把size的返回值赋给一个int变量

3、两个string的相加

  

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{
    string s1("hello, ");
    string s2("world");
    s1+=s2;
    cout << s1;
    return 0;
}
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{
    string s1("hello, ");
    string s2("world");
    string s3;
    s3=s1+s2;
    cout << s3;
    return 0;
}
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{
    string s1("hello");
    string s2("world");
    string s3;
    s3=s1+", "+s2;
    cout << s3;
    return 0;
}

 

output:hello, world

4、cctype头文件定义的函数

isalnum(c)    如果c是字母或数字,则为true.

isalpha(c)    如果c是字母,则为true.

iscntrl(c)     如果c是控制字符,则为true.

isdigit(c)      如果c是数字,则为true。

isgraph(c)    如果c不是空格,但是可以打印,则为true。

islower(c)    如果c是小写字母,

isupper(c)    如果c是大写字母,

ispunct(c)      如果c是标点符号,

isspace(c)    如果c是空白字符,

tolower(c)    大写字母->小写字母

toupper(c)     小写字母->大写字母

 

string

标签:size   打印   字母   string   logs   比较   out   while   return   

原文地址:http://www.cnblogs.com/ljhacm/p/6911735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!