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

c++ string char* 获取输入值的区别

时间:2017-06-11 12:06:56      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:cout   string   return   lin   getline   允许   tput   str   ++   

#include <iostream>  
#include <string>  
using namespace std;  
  
  
void reverseStr(string &s,int begin,int end){  
      
    while (begin < end) {  
        char temp = s[begin];  
        s[begin] = s[end];  
        s[end] = temp;  
        begin++;  
        end--;  
    }  
}  
  
  
int main(){  
  
     //对于char* / char[]  
    char s[1001];  
    cout<<"Please input char[]:"<<endl;  
    cin.getline(s, 1000);//iostream下的函数, 第二个参数表示允许的最大输入长度  
    cout<<"Output:"<<endl<<s<<endl<<strlen(s)<<endl;   
  
    //对于string  
    string ss;  
    cout<<"Please input string:"<<endl;  
    getline(cin, ss); //这个getline函数在<string>头文件下  
    cout<<"Output:"<<endl<<ss<<endl<<ss.length()<<endl; 
	
	int i=0;
	int j=0;
	int icount=0;
	int jcount=0;
	while(s[i]!=‘\0‘){
		cout<<s[i]<<icount<<" ";
		i++;
		icount++;
	}

	cout<<endl;
	while(ss[j]!=‘\0‘){
		cout<<s[j]<<jcount<<" ";
		j++;
		jcount++;
	}

	cout<<endl;
	
    return 0;  
} 

  

c++ string char* 获取输入值的区别

标签:cout   string   return   lin   getline   允许   tput   str   ++   

原文地址:http://www.cnblogs.com/Czc963239044/p/6984696.html

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