使用 find 和 substr 从我的输出中删除逗号

Using find and substr to remove a comma from my output

本文关键字:删除 输出 我的 find substr 使用      更新时间:2023-10-16

我给我的老师发了消息,寻求帮助我的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 ?