用户定义的分隔符或分隔操作?

时间:2016-12-31 13:48:40

标签: c++ string

我希望用户定义分隔符!!但是得到错误就像重载功能和更多的错误!!

string first_operator;
string second_operator;
std::cout << "Please enter the first seperation operator :";
getline(cin, first_operator, ' ');

std::cout << "Please enter the second seperation operator :";
getline(cin, second_operator, ' ');

string first_name;
std::cout << "Please enter your name:";
getline(cin, first_name, first_operator );

string last_name;
getline(cin, last_name, second_operator );

std::cout << first_name << " " << last_name << std::endl;

std::cin.ignore();

1 个答案:

答案 0 :(得分:0)

没有std::getline()版本的std::stringstd::string引用,因为它的&#34;分隔符&#34;参数。

Considering only the version which takes a delimiter, the function std::getline() is declared as follows

// Version 1a.
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,
                                           CharT delim );

// Version 1b (C++11 or later).
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input,
                                           std::basic_string<CharT,Traits,Allocator>& str,
                                           CharT delim );

对于std::string,声明是:

// Version 1a.
std::istream& getline(std::istream& input, std::string& str, char delim);

// Version 1b.
std::istream& getline(std::istream&& input, std::string& str, char delim);

请注意,第三个参数是char。因此,分隔符必须是单个字符,而不是字符串。

考虑到这一点,我们可以按如下方式重写代码:

char first_operator, second_operator;
std::cout << "Please enter the first seperation operator :";
std::cin >> first_operator; // Get first character from input.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.
std::cout << "Please enter the second seperation operator :";
std::cin >> second_operator; // Get first character from input.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.

string first_name;
std::cout << "Please enter your name:";
getline(cin, first_name, first_operator );

string last_name;
getline(cin, last_name, second_operator );

std::cout << first_name << " " << last_name << std::endl;

std::cin.ignore();

请注意,这只允许使用单字符分隔符。如果您想允许多字符分隔符,则需要进行更复杂的处理。

std::string first_operator, second_operator;
std::cout << "Please enter the first seperation operator :";
std::getline(std::cin, first_operator);
std::cout << "Please enter the second seperation operator :";
std::getline(std::cin, second_operator);

// Loop until name is entered correctly.
bool done = false;
do {
    std::string name_buf;
    std::cout << "Please enter your name:";
    std::getline(std::cin, name_buf);

    auto first_op  = name_buf.find(first_operator);
    auto second_op = name_buf.find(second_operator);
    if (first_op == std::string::npos || second_op == std::string::npos) {
        std::cout << "Please separate your first and last names with the first specified operator, "
                  << "and follow your last name with the second specified operator.\n";
        continue; // Restart loop.
    }

    first_name = name_buf.substr(0, first_op);
    auto last_start = first_op + first_operator.size();
    last_name  = name_buf.substr(last_start, second_op - last_start);
    done = true;
} while(!done);

在行动here中查看。忽略&#34;格式化问题;请输入 x &#34;因为Ideone如何处理示例代码中的标准输入而导致的行。

取自xkcd

的名称