string基本字符系列容器(一)

  由于C语言中只提供了一个char类型用来处理字符,对于字符串的处理,我们往往都是通过字符数组来处理的,这有些时候还是很不方便的。

其实vector<char>就和char类型的数组很像,vector<string>就是字符串类型的数组了。

 

1.1 创建一个string类型的对象

  下面的代码创建了一个字符串对象s,且s是一个空串,他的长度是0, 对于string来说,可以同时用s.length()和s.size()来求长度

# include<iostream>
# include<string>

using namespace std;

int main(void)
{
    string s;
    cout<<s.length()<<endl;
    cout<<s.size()<<endl;

    return 0;
}

 

1.2 给string对象赋值

 

  给string赋值有两种方式,一种是直接给string来赋值,另外一种是把字符串指针赋值给一个字符串对象.

  1)代码:

# include<iostream>
# include<string>

using namespace std;

int main(void)
{
    string s;
    s = "Hello,C++STL";
    cout<<s<<endl;
    return 0;
}

2)更常用的方法是把字符串指针赋值给一个字符串对象.

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string ss;
    char s[MAX];
    scanf("%s",s);
    ss = s;
    cout<<ss<<endl;


    return 0;
}

 

2.3 从string对象的尾部添加字符

  在string对象的尾部添加一个字符(char),采用"+"操作即可,具体应用如下:

  

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s+='a';
    s+='b';
    s+='c';
    cout<<s<<endl;

    return 0;
}

在string的尾部当然还可以添加字符串,方法也有两种,一种是用”+“直接上,还有一种是直接用append函数.

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s+="abc";
    s+="123";
    cout<<s<<endl;

    return 0;
}

用append函数来搞定.

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s.append("123");
    s.append("abc");
    cout<<s<<endl;

    return 0;
}

 

2.4 给string对象插入字符

  可以使用insert()方法把一个字符插入到迭代器所指的位置之前的地方。

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s = "1234556";
    //定义迭代器
    string::iterator it;
    //迭代器的位置是字符串首
    it = s.begin();
    //把字符‘p’插入到string对象的第一字符的前面,字符位置是从0开始计数的
    s.insert(it+1,'p');
    cout<<s<<endl;

    return 0;
}

 

2.5 访问string对象的元素

  

  一般使用下标方式随机的访问string对象的元素,下标是从0开始的计数的。另外,string对象中的元素的本质是char,这一点要明确。

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s = "1234556aaaa";
    cout<<s[8]<<endl;
    cout<<s[8]-'a'<<endl;
    return 0;
}

 

2.6 删除string对象的元素

  删除string对象中的元素,其实和删除vector容器中的元素是一样的,都是使用erase()或者给原始的string赋值一个”“(空串即可将它清空).

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s = "1234556aaaa";
    string::iterator it;
    it = s.begin();
    s.erase(it+3);
    cout<<s<<endl;
    s.erase(it,it+5);
    cout<<s<<endl;
    s = "";
    cout<<s.length()<<endl;


    return 0;
}

 

2.7 返回string对象的长度

  采用length()方法可以返回字符串的长度,采用empty()的方法,可以返回字符串是不是为空,如果字符串为空的话,就返回逻辑真,1.

否则,就返回逻辑假,0

# include<iostream>
# include<string>
# include<cstdio>

using namespace std;

# define MAX 123

int main(void)
{
    string s;
    s = "1234556aaaa";
    cout<<s.length()<<endl;
    cout<<s.empty()<<endl;
    s = "";
    cout<<s.length()<<endl;
    cout<<s.empty()<<endl;

    return 0;
}
posted @ 2015-04-06 01:06  BYYB_0506  阅读(200)  评论(0编辑  收藏  举报