具有多个测试用例的 getline 问题
Problem in getline with multiple test cases
我想打印字符串中每个单词的第一个字母。我已经使用 getline 函数来获取带空格的字符串。它适用于单个测试用例,但不适用于多个测试用例。请帮助解释为什么会发生这种情况,并在可能的情况下提出解决方案以获得多个测试用例的答案。
#include<bits/stdc++.h>
using namespace std;
string firstLetterWord(string str) {
string result = "";
if(str[0]!=' ')result.push_back(str[0]);
for (int i=1; i<str.length(); i++) {
if (str[i] != ' ' && str[i-1] == ' ') {
result.push_back(str[i]);
}
}
return result;
}
int main() {
string str;
getline(cin,str);
cout << firstLetterWord(str);
return 0;
}
如果我输入 't' 测试用例的数量,然后找到字符串的答案,那么代码只会给出第一个测试用例的答案。
正如@NathanOliver 评论的那样,getline()
reads every line, while std::cin
reads every word, which is exactly what you need (If you are not convinced, read more in std::cin.getline( ) vs. std::cin)。
让您入门的最简单示例:
#include <iostream>
#include <string>
int main(void)
{
std::string word;
while(std::cin >> word) {
std::cout << word[0] << "\n";
}
return 0;
}
输出(输入:羚羊鸟猫狗):
A
b
c
d
PS:正如@SomeProgrammerDude 提到的:
如果您需要从输入中读取多行,并分别处理它们,那么您可以使用 std::stringstream,如下所示:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(void)
{
int lines_no;
cin >> lines_no;
// To ignore the trailing newline
std::cin.ignore();
while(lines_no--)
{
string line;
// Read a line from the input
getline(cin, line);
// Construct a string stream based on the current line
stringstream ss(line);
string word;
// For every word of the sstream,
while(ss >> word)
// print its first character
cout << word[0];
cout << endl;
}
return 0;
}
输入:
MY NAME IS ANKIT
HELLO HOW ARE YOU
输出:
MNIA
HHAY
PS:我不得不忽略结尾的换行符,如 here 所述。
我想打印字符串中每个单词的第一个字母。我已经使用 getline 函数来获取带空格的字符串。它适用于单个测试用例,但不适用于多个测试用例。请帮助解释为什么会发生这种情况,并在可能的情况下提出解决方案以获得多个测试用例的答案。
#include<bits/stdc++.h>
using namespace std;
string firstLetterWord(string str) {
string result = "";
if(str[0]!=' ')result.push_back(str[0]);
for (int i=1; i<str.length(); i++) {
if (str[i] != ' ' && str[i-1] == ' ') {
result.push_back(str[i]);
}
}
return result;
}
int main() {
string str;
getline(cin,str);
cout << firstLetterWord(str);
return 0;
}
如果我输入 't' 测试用例的数量,然后找到字符串的答案,那么代码只会给出第一个测试用例的答案。
正如@NathanOliver 评论的那样,getline()
reads every line, while std::cin
reads every word, which is exactly what you need (If you are not convinced, read more in std::cin.getline( ) vs. std::cin)。
让您入门的最简单示例:
#include <iostream>
#include <string>
int main(void)
{
std::string word;
while(std::cin >> word) {
std::cout << word[0] << "\n";
}
return 0;
}
输出(输入:羚羊鸟猫狗):
A
b
c
d
PS:正如@SomeProgrammerDude 提到的:
如果您需要从输入中读取多行,并分别处理它们,那么您可以使用 std::stringstream,如下所示:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(void)
{
int lines_no;
cin >> lines_no;
// To ignore the trailing newline
std::cin.ignore();
while(lines_no--)
{
string line;
// Read a line from the input
getline(cin, line);
// Construct a string stream based on the current line
stringstream ss(line);
string word;
// For every word of the sstream,
while(ss >> word)
// print its first character
cout << word[0];
cout << endl;
}
return 0;
}
输入:
MY NAME IS ANKIT
HELLO HOW ARE YOU
输出:
MNIA
HHAY
PS:我不得不忽略结尾的换行符,如 here 所述。