<p style="margin-top: 5px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; line-height: 26px; word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋體, Arial; font-size: 16px;">//***************************************************</p><p style="margin-top: 5px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; line-height: 26px; word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋體, Arial; font-size: 16px;">//更多精彩,歡迎進入:http://shop115376623.taobao.com</p><p style="margin-top: 5px; margin-bottom: 5px; padding-top: 0px; padding-bottom: 0px; line-height: 26px; word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋體, Arial; font-size: 16px;">//***************************************************</p>
</pre><pre name="code" class="cpp">// String2Char2Data.cpp : 定義控制臺應用程序的入口點。
//#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
字符數組換為字符串
// char a[10]="aaaabbbba";
// string s(&a[0],&a[strlen(a)]);//strlen:求字符串長度,測量字符串長度的函數 s(字符數組a開始的地址,字符數組a結束的地址)
// cout<<s<<endl;
// system("pause");// 把字符串轉換為字符數組
// string s="aaaavvva";
// char a[10];
// strncpy(a,s.c_str(),s.length());//strncpy:將字符串src中最多n個字符復制到字符數組dest中
// /*c_str()函數返回一個指向正規C字符串的指針, 內容與本string串相同.
// 這是為了與c語言兼容,在c語言中沒有string類型,故必須通過string類對象的成員函數c_str()把string 對象轉換成c中的字符串樣式。*/
// for(int i=0;i<s.length();i++)
// {
// cout<<a[i]<<" ";
// }
// cout<<endl;
// system("pause");// 把字符數組轉換為數字
// //char a[2] = "a";
// char a[4] = "123";
// int x;
// x=atoi(a);
// // atoi函數原型:
// // int atoi(char *str)
// // 函數用途:
// // 將字符串轉換成一個整數值
// // 輸入參數:
// // str 待轉換為整型數的字符串
// // 返回值:
// // 成功返回轉換后的數值,失敗則返回0.
// cout<<x+1<<endl;
// getchar();
// system("pause");
}