码迷,mamicode.com
首页 > 编程语言 > 详细

C++中字符数组和字符串string

时间:2014-09-19 23:57:46      阅读:662      评论:0      收藏:0      [点我收藏+]

标签:c++   string   字符数组   

字符数组

C++中字符数组用char str[]可以用来表示一个字符串。

(1)   数组的大小和字符串的长度。

数组的大小一定要大于字符串的长度,因为系统会自动补上一个’\0’作为字符串的结束标志。当然对于未初始化的也补’\0’.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	char str[11] = "I am happy";   //  系统会自动补上'\0'空字符作为结束标志,,未有初始化的也补'\0'
	//char str[10] = "I am happy";    // 出错  系统自动补上'\0' 此时字符数组长度不够
	//char str[13] = "I am happy";    //  后面未有初始化的也补上'\0' 为 I am happy\0\0\0
	if(str[10] == '\0')
	{
		cout << "hello world!!" << endl;
	}

	cin >> str;      //  输入输出  
	cout << str << endl;
	return 0;
}

(2)getline()

getline函数可以读取文本或者输入流的一行,此时包括前面输入的空格,只到回车换行才结束

#include <fstream>
#include <iostream>
#include <string>

using namespace std;
int main()
{
     ifstream in("E:\\algorithmZack\\testString\\input.txt");
     if(!in)
     {
          cerr << "some errors happened";
          return -1;
     }
     string str;
	 while(getline(in, str)) ///  getline 从文件input.txt中按行读取文件
    // while(getline(cin, str))   //  从输入流中按行读取  不包括换行符
     {
            cout << str << endl;
     }
     return 0;
}

bubuko.com,布布扣

bubuko.com,布布扣

(3)比较,连接,赋值,实际长度用函数strcmp, strcat, strcpy,strlen

参见blog:http://see.xidian.edu.cn/cpp/biancheng/view/158.html

字符串string

(1)String可以看做一个类库,需要有包含头文件#include <string>.

操作包括:连接(+=,append) 赋值(=, assign) 比较(>=,compare) 查找(find)

              替换(replace)  删除(erase) 插入(insert) 字串(substring) 交换(swap)

              特性(length sizec_str)  正反向迭代器(interatorreverse_iterator)

其中使用append,assign,compare的好处在于参数可以为字符数组

详细见blog:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

(2)一个简单的例子

从txt中读入以空格为单位的字符串,并对其进行去重排序输出

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
	ifstream in("E:\\algorithmZack\\testString\\name.txt");   // fstream
	string str;           
	vector<string> vec;     //vector
	while(getline(in, str))       // string
	{
		while(str.find_first_of(' ') != -1)
		{
			vec.push_back(str.substr(0, str.find_first_of(' ')));
			str = str.substr(str.find_first_of(' ')+1);
		}
		vec.push_back(str);
	}
	sort(vec.begin(), vec.end());           // algorithm
	/*注意这里去掉了重复元素 但vec大小没变,在其后面添加了两个空格。但是对于int,则将重复的元素放在后面 */
	vector<string>::iterator it = unique(vec.begin(), vec.end());   // algorithm  返回去重后最后一个元素
	copy(vec.begin(), it, ostream_iterator<string>(cout, "\n"));     // iterator   ostream_iterator<string>  iterator
	cout << endl;
	copy(vec.begin(), vec.end(), ostream_iterator<string>(cout, "\n"));
	/*for(vector<string>::iterator iter = vec.begin(); iter != vec.end(); iter++)
	{
		cout << *iter << " ";
	}
	cout << endl;*/
	return 0;
}
bubuko.com,布布扣

bubuko.com,布布扣

注意:这里的unique去掉了vector<string>中重复元素,但其大小没有变,重复的元素用空格代替放在其后面。但是对已vector<int>,则将重复的元素放在后面。

(3)copy函数

Copy函数包含在头文件#include<iterator>头文件中。主要有三个常用的用法

copy(IteratorInput it1, IteratorInput it2, IteratorOnputit3)  // algorithm

1: 用数组对vector<T>进行赋值

2:用cin对vector<T>进行赋值

3:将vector<T> 进行输出

copy函数原型解释见blog:http://blog.csdn.net/jerryjbiao/article/details/7376088

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
using namespace std;


int main()
{
	vector<int> vec;
	cout << "hello world!!" << endl;
	//int a[] = {3,2,1,1,2,3};
	//copy(a, a+6, back_inserter(vec));
	//vec.resize(6);
//	copy(a, a+6, vec.begin());
	copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(vec));
	copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	sort(vec.begin(), vec.end());
	vector<int>::iterator it = unique(vec.begin(), vec.end());
	copy(vec.begin(), it, ostream_iterator<int>(cout, " "));   // 输出
	return 0;
}

bubuko.com,布布扣

(4)一个简单的例子

过滤一行开头和结尾的所有的非英文字符。这里只是为了说明find函数find_first_of函数的区别。Find是从pos开始查找字符串s在当前串中的位置;而find_first_of是从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。

#include <string>
#include <iostream>
using namespace std;

int main()
{
	string strinfo = "//*----Hello world!.....----";
	string strset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

	int first = strinfo.find_first_of(strset);
	if(first == string::npos)
	{
		cout << "not find any characters" << endl;
		return -1;
	}

	int last = strinfo.find_last_of(strset);

	if(last == string::npos)         //string::npos =-1
	{
		cout << "not find any characters" << endl;
		return -1;
	}
	cout << strinfo.substr(first, last - first +1) << endl;


	string str = "hello world!!!";
	string str2 = "hlo";
	// 注意 find和find_first_of()区别很大
	int j = str.find(str2);  // 从pos开始查找字符串s在当前串中的位置 
	int i = str.find_first_of(str2);  // 从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。
	cout << i << endl;


	return 0;
}

bubuko.com,布布扣

参见其它blog:1:http://www.cplusplus.com/reference/string/string/:

2:http://blog.csdn.net/yangtrees/article/details/7577263

3:http://www.cnblogs.com/uniqueliu/archive/2011/07/28/2119328.html

4:http://blog.csdn.net/jerryjbiao/article/details/7376088

5:http://www.cnblogs.com/zkliuym/articles/909245.html

C++中字符数组和字符串string

标签:c++   string   字符数组   

原文地址:http://blog.csdn.net/lu597203933/article/details/39404565

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