c++中字符数组与字符串的转换
2017-10-04 09:43
441 查看
此文章转载自http://blog.csdn.net/xinwang24/article/details/6612686
1:字符数组换为字符串
见代码
[cpp] view
plain copy
#include <iostream>
#include <string>
using namespace std;
int main(){
char a[10]="aaaabbbba";
string s(&a[0],&a[strlen(a)]);
cout<<s<<endl;
system("pause");
}
2:把字符串转换为字符数组
见代码:
[cpp] view
plain copy
#include <iostream>
#include <string>
using namespace std;
int main(){
string s="aaaavvva";
char a[10];
strncpy(a,s.c_str(),s.length());
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
}
3:把字符串转换为数字
[cpp] view
plain copy
#include<iostream>
using namespace std;
int main()
{
char a='1';
int x;
x=atoi(a);
cout<<x<<endl;
getchar();
return 0;
}
1:字符数组换为字符串
见代码
[cpp] view
plain copy
#include <iostream>
#include <string>
using namespace std;
int main(){
char a[10]="aaaabbbba";
string s(&a[0],&a[strlen(a)]);
cout<<s<<endl;
system("pause");
}
2:把字符串转换为字符数组
见代码:
[cpp] view
plain copy
#include <iostream>
#include <string>
using namespace std;
int main(){
string s="aaaavvva";
char a[10];
strncpy(a,s.c_str(),s.length());
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
}
3:把字符串转换为数字
[cpp] view
plain copy
#include<iostream>
using namespace std;
int main()
{
char a='1';
int x;
x=atoi(a);
cout<<x<<endl;
getchar();
return 0;
}
相关文章推荐
- c++中字符数组与字符串的转换
- C++ 字符串,字符数组,数字之间转换
- c++中字符数组与字符串的转换
- c++中字符数组与字符串的转换
- c++中字符数组与字符串的转换
- C++ 字符串,字符数组,数字之间转换
- 《C++标准函数库》中说的有三个函数可以将字符串的内容转换为字符数组和C—string (CString、char*、String三种类型互换)
- C/C++——声明一个到字符的指针,包含10个整数的数组,到包含10个整数的数组的引用,到字符串的数组的指针,到字符的指针的指针...
- *Delphi的字符串、PChar和字符数组之间的转换
- 当‘EFBFBD’和它的朋友相遇:研究字符数组转换字符串
- C++中字符数组与string的相互转换
- c/c++各种字符、字符串类型转换
- java中字符串与字符数组之间的转换
- C++指针和数组:字符和字符串、字符数组
- (27)'c++:COMPLETE REFERENCE' 第一部分 第四章(数组和以空字符结束的字符串) 第二节
- java中字符数组和字符串的相互转换
- C++基础之字符数组和字符串
- c++中字符数组与字符串的关系
- C/C++中的字符串和字符数组
- 字符串和字符数组互相转换