English 中文(简体)
• 如何检查门槛值的终点? [闭门]
原标题:How do you check for end of line in c++? [closed]
  • 时间:2011-07-04 08:53:20
  •  标签:
  • c++

I have to accept space separated integers from user but stop when i encounter a new line. For example:

3 5 7 9
23 47
6

In this example I must store 3, 5, 7, 9 in array 1, 23, 47 in array 2, 6 in array 3. Unfortunately, I don t know how to check for the end of line in C++. Please help me out.

问题回答

通常的解决办法是使用<代码>std:getline <>/code>到行文读,然后是std:istringstream to parse each line.

也许不太简单,但这是一部法典。 我多年来一直用在数据档案中阅读。 把它当作一个星体和直径,并将退回一个有限制的星体。 我通常利用它打破界限,但如果你有代表一行之末的奇迹,我就认为适用同样的原则。 我要说的是,我没有写这封信,但我早就忘记了。

Split(std::string& line, std::string& splitter)
{
  std::vector<std::string> result;
  if (!line.empty() && !splitter.empty())
  {
    for (std::string::size_type offset = 0;;)
    {
      std::string::size_type found = line.find(splitter, offset);
      if (found != std::string::npos)
      {
          std::string tmpString = line.substr(offset, found-offset);
          if (tmpString.size() > 0)
          {
                  result.push_back(tmpString);
          }
          offset = found + splitter.size();
      } else {
        std::string tmpString = line.substr(offset, line.size()-offset);

       if (tmpString.size() > 0)
        {
                result.push_back(tmpString);
        }
        break;
      }
    }
  }
  return result;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签