您的位置:首页 > 其它

对string 简单应用

2017-11-26 12:30 302 查看
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main(int argc, char** argv) {
string str = "高德纳 美国 计算机科学家 计算机程序设计艺术";
string str_temp = " ";
str_temp.assign(str);// 字符串复制

string result[4] = { " "," " ," "," " };
int position = 0;
for (int i = 0; i < 3; i++)
{
position = str_temp.find(" ");//第一次出现的位置
result[i] = str_temp.substr(0, position);// 取出字符串从0 开始 position 个字符
str_temp = str_temp.substr(position + 1, str_temp.length() - position); // 删除上述字符
}

result[3] = str_temp;
cout << "姓名: " << setw(8) << result[0] << endl;
cout << "国籍: " << setw(6) << result[1] << endl;
cout << "职业:" << setw(14) << result[2] << endl;
cout << "代表作 : " << setw(18) << result[3] << endl;

str_temp.swap(result[0]);    // 将本字符串与result 进行交换

for (int j = 1; j < 4; j++)
{
str_temp += " ";
str_temp.append(result[j]);  //  还原操作  将result 追加到本字符串末尾
}
int equal = str.compare(str_temp);  // 将两个字符串进行比较  相同  结果为零

if (equal == 0)
{
cout << "Success Matching!" << endl;
}
else
cout << "Unsuccessful Matching!" << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: