设为首页 加入收藏

TOP

C++中字符数组和字符串string
2015-07-20 17:39:02 来源: 作者: 【 】 浏览:3
Tags:字符 字符串 string

字符数组

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

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

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

?

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

?

(2)getline()

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

?

#include 
  
   
#include 
   
     #include 
    
      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; }
    
   
  

?

\

\

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

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

字符串string

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

操作包括:连接(+=,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 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; int main() { ifstream in(E:\algorithmZack\testString\name.txt); // fstream string str; vector
        
          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
         
          ::iterator it = unique(vec.begin(), vec.end()); // algorithm 返回去重后最后一个元素 copy(vec.begin(), it, ostream_iterator
          
           (cout, )); // iterator ostream_iterator
           
             iterator cout << endl; copy(vec.begin(), vec.end(), ostream_iterator
            
             (cout, )); /*for(vector
             
              ::iterator iter = vec.begin(); iter != vec.end(); iter++) { cout << *iter << ; } cout << endl;*/ return 0; }
             
            
           
          
         
        
       
      
     
    
   
  
\

?

\

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

(3)copy函数

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

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

1: 用数组对vector 进行赋值

2:用cin对vector 进行赋值

3:将vector 进行输出

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

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; int main() { vector
       
         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
        
         (cin), istream_iterator
         
          (), back_inserter(vec)); copy(vec.begin(), vec.end(), ostream_iterator
          
           (cout, )); cout << endl; sort(vec.begin(), vec.end()); vector
           
            ::iterator it = unique(vec.begin(), vec.end()); copy(vec.begin(), it, ostream_iterator
            
             (cout, )); // 输出 return 0; }
            
           
          
         
        
       
      
     
    
   
  

?

\

(4)一个简单的例子

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

?

#include 
  
   
#include 
   
     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; }
   
  

?

\

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇(十四)unity4.6学习Ugui中文文.. 下一篇CATransition过渡动画

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·「链表」是一种怎样 (2025-12-25 19:20:51)
·C 语言中的链表有哪 (2025-12-25 19:20:48)
·c语言中的链表怎么学 (2025-12-25 19:20:45)
·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)