解决方案
C到C++的进化过程引入了自定义类型
在C++中可以通过类完成字符串类型的定义
标准库中的字符串类
C++语言支持C语言的所有概念
C++语言中没有原生的字符串类型
C++标准库提供了string类型
string直接支持字符串的连接
string直接支持字符串的大小比较
string直接支持子串查找和提取
string直接支持字符串的插入和替换
例:
#include <iostream>
#include <string>
using namespace std;
void string_sort(string a[], int len)
{
for(int i=0; i<len; i++)
{
for(int j=i; j<len; j++)
{
if( a[i] > a[j] )
{
swap(a[i], a[j]);
}
}
}
}
string string_add(string a[], int len)
{
string ret = "";
for(int i = 0;i<len;i++)
{
ret += a[i]+";";
}
cout <<endl;
return ret;
}
int main()
{
string sa[7] =
{
"Hello World",
"D.T.Software",
"C#",
"Java",
"C++",
"Python",
"TypeScript"
};
string_sort(sa, 7);
for(int i=0; i<7; i++)
{
cout << sa[i] << endl;
}
cout << endl;
cout << string_add(sa, 7) << endl;
return 0;
}
输出:
C#
C++
D.T.Software
Hello World
Java
Python
TypeScript
C#;C++;D.T.Software;Hello World;Java;Python;TypeScript;
字符串与数字的转换
标准库中提供了相关的类对字符串和数字之间的转换
字符串流类(sstream)用于string的转换
#### <sstream>-相关的头文件
#### istringstream-字符串输入流
#### ostringstream-字符串输出流
例:
#include "iostream"
#include "sstream"
#include "string"
using namespace std;
int main()
{
istringstream iss("123.45");
double num;
iss >> num;
cout << num<< endl;
ostringstream oss;
oss<<543.21;
string s = oss.str();
cout<< oss.str()<<endl;
return 0;
}
输出:
123.45
543.21
#include "iostream"
#include "sstream"
#include "string"
using namespace std;
bool to_numb(const string& s, int& n)
{
istringstream iss(s);
return iss >> n;
}
bool to_numb(const string& s, double& n)
{
istringstream iss(s);
return iss >> n;
}
bool to_numb(const string& s, float& n)
{
istringstream iss(s);
return iss >> n;
}
int main()
{
int n = 0;
cout << to_numb("234",n) << endl; //1
cout << n << endl;
istringstream iss("123.45");
double num;
if(iss >> num) //bool
{
cout << num << endl;
}
ostringstream oss;
//oss << 543.21;
oss << 543<<"."<<21;
string s = oss.str();
cout<< oss.str() << endl;
return 0;
}
输出:
1
234
123.45
543.21
例:宏定义版本
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define TO_NUMBER(s, n) (istringstream(s) >> n)
#define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str())
int main()
{
double n = 0;
if( TO_NUMBER("234.567", n) )
{
cout << n << endl;
}
string s = TO_STRING(12345);
cout << s << endl;
return 0;
}
输出:
234.567
12345
例:字符串拼接
#include <iostream>
#include <string>
using namespace std;
string right_func(const string& s,unsigned int n)
{
string ret = "";
unsigned int pos = 0;
n = n%s.length(); //返回当前字符串的长度
pos = s.length() - n;
ret = s.substr(pos);//提取子串
ret += s.substr(0,pos);
//abcdefg==>8
//abcdefg==>1
//8%7 = 1
//7-1 = 6
//abcdef g
//ret = g
//ret = g+abcdef
//ret = gabcdef
return ret;
}
int main()
{
cout<<right_func("abcedfg",8)<<endl;
return 0;
}
输出结果:gabcedf
例:写成函数重载
#include <iostream>
#include <string>
using namespace std;
string operator >>(const string& s,unsigned int n)
{
string ret = "";
unsigned int pos = 0;
n = n%s.length(); //返回当前字符串的长度
pos = s.length() - n;
ret = s.substr(pos);//提取子串
ret += s.substr(0,pos);
//abcdefg==>8
//abcdefg==>1
//8%7 = 1
//7-1 = 6
//abcdef g
//ret = g
//ret = g+abcdef
//ret = gabcdef
return ret;
}
int main()
{
string s = "abcdefg";
string r = (s >> 8);
cout<< r <<endl;
//cout<<right_func("abcedfg",8)<<endl;
return 0;
}
输出结果:gabcedf
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。