`
abc20899
  • 浏览: 910700 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

数组与string

 
阅读更多
#include <iostream.h>
#include <string.h>
using namespace std;

int main(){
    int Arr[5];

//    for(int i = 0; i < 5; i++){
//      cout <<"the Arr :["<< i <<"]"<<endl;
//      cin >> Arr[i];   //存入到数组里面
//    }
//
//    for(int i=0; i<5; i++){
//    	cout <<i<<":"<<Arr[i]<<endl;
//    }


//    int Arr2[5] = {2,3};
//    for(int i=0;i<5;i++){
//    	cout <<i<<":"<<Arr2[i]<<endl;
//    }
//    char greeting[] = "hello world";  // 有12个字节 \0


//    char buffer[80] = {'\0'};
//    cout << "enter the string";
//    cin >>buffer;
//    cout << "here is the buffer:"<<endl<< buffer << endl;
    //存在问题: 若多与79个字符则超出缓冲区,若输入空格则停止向缓冲区存储。
    //cin.get()方法,待填充的缓冲区   读取的最大字符数   终止的限定符(默认为换行符)

//    char buffer[80] = {'\0'};
//    cout << "enter the string"<< endl;
//    cin.get(buffer,79);
//    cout << "here is the buffer:"<<endl<< buffer << endl;


    // 函数strcpy() strncpy() 在string.h文件中  必须包含
//    char String1[] = "no man is an island";
//    char String2[80] = "\0";
//    strcpy(String2,String1);
//    cout << "String1:"<<String1<<endl;
//    cout << "String2:" <<String2<<endl;

//    const int MaxLength = 80;
//    char String1[] = "no man is an island";
//    char String2[MaxLength+1] = "\0";
//    strncpy(String2,String1,MaxLength);
//    cout << "String1:"<<String1<<endl;
//    cout << "String2:" <<String2<<endl;


     string str1 = "hello string";
     cout << "str1:"<<str1<<endl;
     string str2;
     str2 = str1;
     cout << "str2:"<<str2<<endl;
     str2 = "this is c++ string";
     cout << "str2:"<<str2<<endl;
     string str3 = str1+str2;
     cout << "str3:"<<str3<<endl;
     //string 类子幕后为程序员管理内存和复制数据

//      unsigned short someArr[5][4];
//      for(int i=0; i<5; i++){
//    	  for(int j=0; j<4; j++){
//    		  someArr[i][j] = 0;
//    	  }
//      }

	return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics