C ++不能减去两个字符串 [英] C++ Can't subtract two strings
本文介绍了C ++不能减去两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在这段代码中减去两个字符串,但它不会让我这样做,并给出一个运算符 - 错误。这个代码基本上试图将一个完整的输入名称分成两个输出:名字和姓氏。请帮忙!谢谢!
I want to subtract two strings in this code, but it won't let me do it and gives an operator - error. This code basically tries to separate a full input name into two outputs: first and last names. Please Help! Thank you!
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
string employeeName, firstName, lastName;
int pos1, difference;
int main() {
cout << "Enter your full name: " << endl;
getline(cin, employeeName);
pos1 = employeeName.find(" ");
difference = pos1 - 0;
lastName = employeeName.erase(0,difference);
firstName = employeeName - lastName;
cout << lastName << firstName << endl;
system("pause");
return 0;
}
推荐答案
$ c> std :: string :: substr 。减去这样的字符串无效。
You should use std::string::substr
. Subtracting strings like that is invalid.
firstName = employeeName.substr(0, employeeName.find(" "));
第一个参数是要提取的子字符串的起始索引,第二个参数是长度的子字符串。
The first parameter is the starting index of the substring you want to extract and the second parameter is the length of the substring.
这篇关于C ++不能减去两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文