整数分词器

发布于 2024-07-27 22:48:05 字数 211 浏览 2 评论 0原文

我知道有字符串标记器,但是有“int 标记器”吗?

例如,我想拆分字符串“12 34 46”并具有:

列表[0]=12

列表[1]=34

列表[2]=46

特别是,我想知道 Boost::Tokenizer 是否这样做。 尽管我找不到任何不使用字符串的示例。

I know there are string tokenizers but is there an "int tokenizer"?

For example, I want to split the string "12 34 46" and have:

list[0]=12

list[1]=34

list[2]=46

In particular, I'm wondering if Boost::Tokenizer does this. Although I couldn't find any examples that didn't use strings.

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

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

发布评论

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

评论(4

╄→承喏 2024-08-03 22:48:21

我不确定您是否可以在不使用 string 或 char* 的情况下执行此操作,因为您必须将数字和空格放入同一组中...

i am not sure if you can do this without using string or char* because you have to but both numbers and spaces into same set...

风吹短裙飘 2024-08-03 22:48:19

您正在寻找的是 2 个单独的操作。 首先对字符串进行标记,然后将每个标记转换为 int。

What you're looking for is 2 separate actions. First tokenize the string, then convert each token to an int.

深居我梦 2024-08-03 22:48:16

是的,有:使用流,例如 stringstream

stringstream sstr("12 34 46");
int i;
while (sstr >> i)
    list.push_back(i);

或者,您也可以将 STL 算法和/或迭代器适配器与构造函数结合使用:

vector<int> list = vector<int>(istream_iterator<int>(sstr), istream_iterator<int>());

Yes there is: use a stream, e.g. a stringstream:

stringstream sstr("12 34 46");
int i;
while (sstr >> i)
    list.push_back(i);

Alternatively, you can also use STL algorithms and/or iterator adapters combined with constructors:

vector<int> list = vector<int>(istream_iterator<int>(sstr), istream_iterator<int>());
私藏温柔 2024-08-03 22:48:13

C++ 字符串工具包库 (StrTk) 针对您的问题提供了以下解决方案:

#include <string>
#include <deque>
#include "strtk.hpp"

int main()
{ 
   {
      std::string data = "12 34 46";
      std::deque<int> int_list;
      strtk::parse(data," ",int_list);
   }

   {
      std::string data = "12.12,34.34|46.46 58.58";
      std::deque<double> double_list;
      strtk::parse(data," ,|",double_list);
   }

   return 0;
}

更多示例可以在此处找到。

注意:解析过程非常 快速高效,让基于 stdlib 和 boost 的解决方案相形见绌。

The C++ String Toolkit Library (StrTk) has the following solution to your problem:

#include <string>
#include <deque>
#include "strtk.hpp"

int main()
{ 
   {
      std::string data = "12 34 46";
      std::deque<int> int_list;
      strtk::parse(data," ",int_list);
   }

   {
      std::string data = "12.12,34.34|46.46 58.58";
      std::deque<double> double_list;
      strtk::parse(data," ,|",double_list);
   }

   return 0;
}

More examples can be found Here

Note: The parsing process is EXTREMELY fast and efficient, putting stdlib and boost based solutions to shame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文