使用 find 和 substr 从我的输出中删除逗号
Using find and substr to remove a comma from my output
我给我的老师发了消息,寻求帮助我的cin.ignore ,这就是她给我回的消息。问题是,当你最后读到时——它包括逗号。所以你的 cin.ignore 需要 2 个参数,100,。在这种情况下,您不需要cin.ignore。先读,再读中间。然后使用字符串函数"查找"逗号,并创建一个从位置 0 开始到逗号之前的位置的子字符串。问题是我不知道她在说什么。我知道如何输入查找函数,但第二部分我没有得到。 所以我在我的程序中放置了查找和 substr 显然不起作用
#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
string last, first, middle;
cout << "Enter in this format your Last name comma First name Middle name. " << endl;
// Input full name in required format
last.find(",");
last.substr(0);
cin >> last;
// receiving the input Last name
cin >> first;
// receiving the input First name
cin >> middle;
// receiving the input Middle name
cout << first << " " << middle << " " << last;
// Displaying the inputed information in the format First Middle Last name
cin >> chr;
return 0;
}
你似乎在这里有一些基本的误解。
last.find(",");
和last.substr(0);
都不会自己做任何事情。它们都返回一个结果,但如果您不将其分配给任何内容,则该结果将丢失。此外,substr
有两个论点。如果省略第二个,last.substr(0)
将简单地返回所有last
。
你的导师的意思可能是last = last.substr( 0, last.find(",") );
。请注意,这必须在您从 cin
读取字符串后发生。让我们把这句话拆开:
-
last.find(",")
将返回逗号所在的位置 -
last.substr( 0, last.find(",") )
是逗号前last
的一部分 - 最后,作业
last = ...
将确保last
实际更改。
请注意,这仅在逗号直接附加到姓氏时才有效(如 "Miller, John Henry"
)。
好的,我认为这就是你想要的:
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
// Read a line from stdin
string line;
getline(cin, line);
vector<string> str_vec;
boost::split(str_vec, line, boost::is_any_of(" ,"));
if (str_vec.size() == 3) {
string last = str_vec[0];
string first = str_vec[1];
string middle = str_vec[2];
std::cout << "First=" << first << " Last=" << last << " Middle=" << middle << endl;
}
}
如果您不想使用(或没有)提升库,可以改为查看以下版本。注释应该可以帮助您理解代码的每个块。
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Read a line from stdin
string line;
getline(cin, line);
// Erase all commas
while (true) {
size_t comma_pos = line.find(",");
if (comma_pos != std::string::npos) {
line.erase(comma_pos, 1);
line.insert(comma_pos, " ");
}
else break;
}
// Change all two spaces to only one space
while (true) {
size_t space_pos = line.find(" ");
if (space_pos != std::string::npos)
line.erase(space_pos, 2);
else break;
}
// Tokenize the names
size_t end_last_pos = line.find(" ");
if (end_last_pos == string::npos) {
cout << "Missing last name" << endl;
return 1;
}
string last = line.substr(0, end_last_pos);
size_t end_first_pos = line.find(" ", end_last_pos + 1);
if (end_first_pos == string::npos) {
cout << "Missing first name" << endl;
return 1;
}
string first = line.substr(end_last_pos + 1, end_first_pos - end_last_pos - 1);
size_t end_middle_pos = line.find(" ", end_first_pos + 1);
string middle;
if (end_middle_pos == string::npos)
middle = line.substr(end_first_pos + 1);
else
middle = line.substr(end_first_pos + 1, end_middle_pos - end_first_pos - 1);
// Print the names
std::cout << "First='" << first << "' Last='" << last << "' Middle='" << middle << "'" << endl;
}
也许你可以试着把你的
last.find(",");
last.substr(0);
在你的cin ?
相关文章:
- 从排序数组中删除重复项,具有不同代码方式的相同解决方案具有不同的输出
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- 在输出之前删除匹配字符
- 提升进程间共享内存删除、权限和输出文件
- 如何从C ++的输出中删除垃圾数据(垃圾数据)
- 使用数组和布尔值在输出未知时从输出中删除逗号
- 从 c++ 输出中删除逗号
- 返回容器时删除输出参数
- 如何从输出中删除这些额外的箭头
- 我写了一个函数来删除循环链接列表中的第 1 个节点,但输出显示无限次"55 44 33 22 11 99",那么我该如何解决呢?
- 如何删除模板类的输出操作程序重载的错误
- 删除"超出矢量误差范围"的打印语句后输出不同的根本原因
- 如何删除Bitblt的输出?(MFC)
- 在队列中,插入和删除后,被删除的元素也会出现在输出中.为什么
- C++输出流删除存储的数据
- 从数组中删除未使用的内存,以免输出随机数
- 如何在先前读取行中找到一些单词并在C 中的输出读/写字符串中删除它
- 使用命令行标志删除Google测试锅炉框架输出
- 无法删除输出后的随机数
- 当我为第一个字符串(C++)输入空格时,如何删除输出中的双引号和逗号