字符串流到向量

发布于 2025-01-01 23:48:16 字数 538 浏览 4 评论 0原文

我想知道从 std::stringstream 写入 vector 的最佳方法是什么。

以下是 stringstream 中内容的示例: "31 #00 532 53 803 33 534 23 37"

这就是我得到的:

int buffer = 0;
vector<int> analogueReadings;
stringstream output;

 while(output >> buffer)
     analogueReadings.push_back(buffer);

然而,似乎发生的是,它读取第一件事,然后它到达 #00 code> 并返回 0 因为它不是数字。

理想情况下,我想要的是,它到达 # 然后跳过所有字符直到下一个空格。这可以通过标志或其他东西实现吗?

谢谢。

I'm wondering what the best way to write from an std::stringstream into a vector<int>.

Here's an example of what's in the stringstream:
"31 #00 532 53 803 33 534 23 37"

Here's what I've got:

int buffer = 0;
vector<int> analogueReadings;
stringstream output;

 while(output >> buffer)
     analogueReadings.push_back(buffer);

However what seems to happen is, it reads the first thing, then it gets to #00 and returns 0 because it's not a number.

Ideally, what I want is, it gets to a # and then just skips all characters until the next whitespace. Is this possible with flags or something?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南薇 2025-01-08 23:48:16
#include <iostream>
#include <sstream>
#include <vector>

int main ( int, char ** )
{
    std::istringstream reader("31 #00 532 53 803 33 534 23 37");
    std::vector<int> numbers;
    do
    {
        // read as many numbers as possible.
        for (int number; reader >> number;) {
            numbers.push_back(number);
        }
        // consume and discard token from stream.
        if (reader.fail())
        {
            reader.clear();
            std::string token;
            reader >> token;
        }
    }
    while (!reader.eof());

    for (std::size_t i=0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << std::endl;
    }
}
#include <iostream>
#include <sstream>
#include <vector>

int main ( int, char ** )
{
    std::istringstream reader("31 #00 532 53 803 33 534 23 37");
    std::vector<int> numbers;
    do
    {
        // read as many numbers as possible.
        for (int number; reader >> number;) {
            numbers.push_back(number);
        }
        // consume and discard token from stream.
        if (reader.fail())
        {
            reader.clear();
            std::string token;
            reader >> token;
        }
    }
    while (!reader.eof());

    for (std::size_t i=0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << std::endl;
    }
}
幻想少年梦 2025-01-08 23:48:16

您需要测试是否有号码。使用这里的答案:

如何确定是否字符串是 C++ 中的数字?

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

bool is_number(const std::string& s){
   std::string::const_iterator it = s.begin();
   while (it != s.end() && std::isdigit(*it)) ++it;
   return !s.empty() && it == s.end();
}
int main ()
{
    vector<int> analogueReadings;
    std::istringstream output("31 #00 532 04hello 099 53 803 33 534 23 37");

    std::string tmpbuff;
    while(output >> tmpbuff){
      if (is_number(tmpbuff)){
         int num;
         stringstream(tmpbuff)>>num;
         analogueReadings.push_back(num);
       }
    }
}

结果是 31 532 99 53 803 33 534 23 37

另外,使用的重要缺点这里描述了这样的词汇转换:
如何在 C++ 中将字符串解析为 int?< /a> ,其中给出了 tringstream(tmpbuff)>>num 的替代方案。

例如04hello变成4,7.4e55变成7。还有可怕的下溢和下溢问题。 André Caron 的干净解决方案已转换

25 10000000000 77 0 0

25 0 0 

我的系统。请注意,77 也丢失了!

You need to test if you got a number or not. use the answer from here:

How to determine if a string is a number with C++?

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

bool is_number(const std::string& s){
   std::string::const_iterator it = s.begin();
   while (it != s.end() && std::isdigit(*it)) ++it;
   return !s.empty() && it == s.end();
}
int main ()
{
    vector<int> analogueReadings;
    std::istringstream output("31 #00 532 04hello 099 53 803 33 534 23 37");

    std::string tmpbuff;
    while(output >> tmpbuff){
      if (is_number(tmpbuff)){
         int num;
         stringstream(tmpbuff)>>num;
         analogueReadings.push_back(num);
       }
    }
}

the result is 31 532 99 53 803 33 534 23 37

Also, important drawbacks of using lexical casts like this is described here:
How to parse a string to an int in C++? , where an alternative to tringstream(tmpbuff)>>num is given.

For example 04hello becomes 4 and 7.4e55 becomes 7. There are also terrible problems with underflow and underflow. The clean solution by André Caron converts

25 10000000000 77 0 0

into

25 0 0 

on my system. Note that also 77 is missing!

情感失落者 2025-01-08 23:48:16

无循环版本:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>

using namespace std;

class IntegerFiller
{
    vector<int> &m_vec;
public:
    IntegerFiller(vector<int> &vec): m_vec(vec) {}

    void operator()(const std::string &str)
    {
        stringstream ss(str);
        int n;
        ss >> n;
        if ( !ss.fail() )
            m_vec.push_back(n);
    }
};

int main()
{
    vector<int> numbers;
    IntegerFiller filler(numbers);
    for_each(istream_iterator<string>(cin), istream_iterator<string>(), filler);
    copy(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " "));
    return 0;
}

No loops version:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>

using namespace std;

class IntegerFiller
{
    vector<int> &m_vec;
public:
    IntegerFiller(vector<int> &vec): m_vec(vec) {}

    void operator()(const std::string &str)
    {
        stringstream ss(str);
        int n;
        ss >> n;
        if ( !ss.fail() )
            m_vec.push_back(n);
    }
};

int main()
{
    vector<int> numbers;
    IntegerFiller filler(numbers);
    for_each(istream_iterator<string>(cin), istream_iterator<string>(), filler);
    copy(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " "));
    return 0;
}
无人问我粥可暖 2025-01-08 23:48:16

我认为最好的方法是

#include <string>
#include <sstream>
#include <vector>

using namespace std;   

int main()
{   
  string numbers = "23,24,27,28";   
  vector<int> integers;   
  stringstream s(numbers);   
  char ch;   
  int a;   
  while(s>>a>>ch) integers.push_back(a);   //s >> reads int char pair 
  s>>a;                                    // reads the last int
  integers.push_back(a); 

  for(int i = 0; i < integers.size(); i++) cout << integers[i] << "\n";
}

I think the best method would be to

#include <string>
#include <sstream>
#include <vector>

using namespace std;   

int main()
{   
  string numbers = "23,24,27,28";   
  vector<int> integers;   
  stringstream s(numbers);   
  char ch;   
  int a;   
  while(s>>a>>ch) integers.push_back(a);   //s >> reads int char pair 
  s>>a;                                    // reads the last int
  integers.push_back(a); 

  for(int i = 0; i < integers.size(); i++) cout << integers[i] << "\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文