Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++_Arrays_Algorithm_Parsing - Fatal编程技术网

C++ 将字符串解析为整数数组

C++ 将字符串解析为整数数组,c++,arrays,algorithm,parsing,C++,Arrays,Algorithm,Parsing,我正在寻找一种方法,将带有指定除法器(如斜杠或空格)的字符串转换为由这些除法器分隔的整数数组 例如,如果用户输入12/3/875/256,我需要检索数组{12,3,875,256}。理想情况下,它将能够处理任意长度 我尝试一个字符一个字符地扫描字符串,并将所有不是除法器的内容存储在一个临时变量中,下次遇到除法器字符时,该变量会添加到数组中。不幸的是,类型转换是一个棘手的问题。有更简单的方法吗?strtok和strtol?(这有点开玩笑。Strtok通常不是个好主意) 本节介绍了拆分 在C++中转

我正在寻找一种方法,将带有指定除法器(如斜杠或空格)的字符串转换为由这些除法器分隔的整数数组

例如,如果用户输入
12/3/875/256
,我需要检索数组
{12,3,875,256}
。理想情况下,它将能够处理任意长度

我尝试一个字符一个字符地扫描字符串,并将所有不是除法器的内容存储在一个临时变量中,下次遇到除法器字符时,该变量会添加到数组中。不幸的是,类型转换是一个棘手的问题。有更简单的方法吗?

strtok和strtol?(这有点开玩笑。Strtok通常不是个好主意)

本节介绍了拆分

在C++中转换字符串为int有很多相关问题

类型转换有什么问题?就我所见,这似乎不是一个街区


您可以显示您的代码吗?

尝试使用和

您可以将“/”设置为分隔符并使用getline读取?然后你必须把每一个都放到一个变量中,你需要知道大小——也许你可以传递数组并计算斜杠?然后你就知道了,可以先设置数组。您可能需要将每个字符串段解析为int,这可能很难,也可能不难。(暂时没有使用C++,我不记得一个方便的方法) 有关如何实现这一点的小示例,请参见(3篇文章)

看一看。它甚至有一个使用boost::tokenizer的标记器代码示例

编辑:

我在那里复制了代码,并做了必要的修改:

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <iterator>
#include <algorithm>

using namespace std;
using namespace boost;

int main(int argc, char** argv)
{
   string text = "125/55/66";
   vector<int> vi;

   char_separator<char> sep("/");
   tokenizer<char_separator<char> > tokens(text, sep);
   BOOST_FOREACH(string t, tokens)
   {
      vi.push_back(lexical_cast<int>(t));
   }

   copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, "\n"));
}

您可以使用和的组合,以任意分隔符分隔字符串,然后可以对其进行词法转换

#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>


#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::string s = "11/23/2010";
    std::vector<std::string> svec;
    std::vector<int> ivec;

    // split the string 's' on '/' delimiter, and the resulting tokens
    // will be in svec.
    boost::split(svec, s, boost::is_any_of("/"));

    // Simple conversion - iterate through the token vector svec
    // and attempt a lexical cast on each string to int
    BOOST_FOREACH(std::string item, svec)
    {
        try
        {
            int i = boost::lexical_cast<int>(item);
            ivec.push_back(i);
        }
        catch (boost::bad_lexical_cast &ex)
        {
            std::cout << ex.what();
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
std::string s=“11/23/2010”;
std::向量svec;
std::向量ivec;
//拆分“/”分隔符上的字符串“s”,以及生成的标记
//将在svec。
boost::split(svec,s,boost::是(“/”)中的任意一个);
//简单转换-迭代令牌向量svec
//并尝试将每个字符串的词法转换为int
BOOST_FOREACH(标准::字符串项,svec)
{
尝试
{
inti=boost::词法转换(item);
ivec.推回(i);
}
捕获(boost::bad_词法_cast&ex)
{

std::它是否会将NUL写入正在解析的字符串中(因此会导致字符串文本出现异常),并且在任何时候都只能有一个strtok在进行中(因为传递NIL作为要标记化的字符串意味着“在上次停止的地方继续”)。@Maxpm:它不是可重入的,也不是类型安全的。总之,它不是C++-ish。
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>


#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::string s = "11/23/2010";
    std::vector<std::string> svec;
    std::vector<int> ivec;

    // split the string 's' on '/' delimiter, and the resulting tokens
    // will be in svec.
    boost::split(svec, s, boost::is_any_of("/"));

    // Simple conversion - iterate through the token vector svec
    // and attempt a lexical cast on each string to int
    BOOST_FOREACH(std::string item, svec)
    {
        try
        {
            int i = boost::lexical_cast<int>(item);
            ivec.push_back(i);
        }
        catch (boost::bad_lexical_cast &ex)
        {
            std::cout << ex.what();
        }
    }

    return 0;
}