C++学习笔记

来源:互联网 发布:什么软件可以下载msqrd 编辑:程序博客网 时间:2024/06/01 15:08

C++学徒01


建立这个博客是为了记录自己的学习笔记,读研期间不再浪费时间,GO!

  1. 标准库string类型
    首先string的使用是需要先声明#include<string>
    string的初始化:
string s1;s1 = "hello world";//也可以string s1 = "hello world";string s2(s1);//将s2初始化为s1的副本string s3("hello world");string s4(n,'C');//将s4初始化为n个C

对于未知数目的string可以采用

string strWord;while(cin>>strWord){//这里的输入不是一行,遇到空格只会读入空格前,如hello world只会读入hello}while(getline(cin,strWord)){//这里读入的是一行,hello world 则为hello world    }//以上均为Ctrl+Z可以输入终止输入符OR达到内部终止条件
  • string的操作
    s.empty()//if s is empty,then return true else if return false
    s.size()//return the character number of s,建议使用string::size_type类型来存储size()返回的值
    s[n]//s[index]index从0开始
    s1+s2//’+’两边必须有一个是String
string s1 = "hello";string s2 = "world";string s3 = s1 +s1;//对string s3 = "hello" + "world";//errorstring s4 = "hello" + s2;//对for(string::iterator it = s1.begin();it != s1.end();it++){    cout<<*it;}//iterator遍历for(string::size_type st = 0;st != s1.size();st++){    cout<<s1[st];}//下标遍历.注意区别

———-待续…

0 0