Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用c++;_C++ - Fatal编程技术网

C++ 使用c++;

C++ 使用c++;,c++,C++,我有一个包含多行的std::string,需要逐行阅读。 请给我举一个小例子来说明如何做到这一点 例:我有一个字符串字符串h h将是: Hello there. How are you today? I am fine, thank you. 我需要提取你好。,你今天好吗?,和我很好,谢谢。不知怎的。这将帮助你: #包括 #包括 int main(){ std::istringstream f(“第1行\nline2\nline3行”); std::字符串行; while(std::getli

我有一个包含多行的
std::string
,需要逐行阅读。 请给我举一个小例子来说明如何做到这一点

例:我有一个字符串
字符串h

h将是:

Hello there.
How are you today?
I am fine, thank you.
我需要提取
你好。
你今天好吗?
,和
我很好,谢谢。
不知怎的。

这将帮助你:
#包括
#包括
int main(){
std::istringstream f(“第1行\nline2\nline3行”);
std::字符串行;
while(std::getline(f,line)){

有几种方法可以做到这一点

您可以在循环中使用
std::string::find
“\n”
字符和位置之间的substr()

您可以使用
std::istringstream
std::getline(istr,line)
(可能是最简单的)


如果您不想使用流,可以使用
boost::tokenize

int main() {
  string out = "line1\nline2\nline3";
  size_t start = 0;
  size_t end;
  while (1) {
    string this_line;
    if ((end = out.find("\n", start)) == string::npos) {
      if (!(this_line = out.substr(start)).empty()) {
        printf("%s\n", this_line.c_str());
      }

      break;
    }

    this_line = out.substr(start, end - start);
    printf("%s\n", this_line.c_str());
    start = end + 1;
  }
}

我正在寻找一个函数的标准实现,该函数可以从字符串返回特定的行。我遇到了这个问题,接受的答案非常有用。我也有自己的实现,我想与大家分享:

// CODE: A
std::string getLine(const std::string& str, int line)
{
    size_t pos = 0;
    if (line < 0)
        return std::string();

    while ((line-- > 0) and (pos < str.length()))
        pos = str.find("\n", pos) + 1;
    if (pos >= str.length())
        return std::string();
    size_t end = str.find("\n", pos);
    return str.substr(pos, (end == std::string::npos ? std::string::npos : (end - pos + 1)));
}
两种实现之间存在行为差异。
code:B
从返回的每一行中删除换行符。
code:A
不删除换行符

我对这个非活动问题的回答是为了让其他人看到可能的实现。

注意:


我不想要任何形式的优化,我想执行一项在Hackathon中交给我的任务!

当你说
multiple link
时,你是真的这么说还是说
?请看“cplusplus.com”的“我如何在这里提问?”部分的最后一段有很多错误(虽然我在那页上看不到任何明显的内容),而且使用全局
std::getline
更好,这样您就可以使用
std::string
。流当然更容易,但有时您有一个不允许流的编码标准:)
// CODE: A
std::string getLine(const std::string& str, int line)
{
    size_t pos = 0;
    if (line < 0)
        return std::string();

    while ((line-- > 0) and (pos < str.length()))
        pos = str.find("\n", pos) + 1;
    if (pos >= str.length())
        return std::string();
    size_t end = str.find("\n", pos);
    return str.substr(pos, (end == std::string::npos ? std::string::npos : (end - pos + 1)));
}
// CODE: B
std::string getLine(const std::string& str, int lineNo)
{
    std::string line;
    std::istringstream stream(str);
    while (lineNo-- >= 0)
        std::getline(stream, line);
    return line;
}