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

C++ 解析C+中的字符串+;通过多个分隔符

C++ 解析C+中的字符串+;通过多个分隔符,c++,arrays,string,parsing,C++,Arrays,String,Parsing,我有一个字符串对象,类似于: string test = " [3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20] " 我试图把它解析成3个独立的数组,分别等于[3,4,8,10,10],[12]和[12,10,20]。我以前已经将逗号分隔的整数解析为数组,但如何解析这个数组呢。不幸的是,我拥有的数据可以在数组中间有换行符,否则我会使用“getline”函数(在将文件读入字符串时)而忽略括号 似乎我需要首先将每个数组放入由括号分隔的字符串中,然后通过逗号分隔将每个数组

我有一个字符串对象,类似于:

string test = "
[3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20]
"
我试图把它解析成3个独立的数组,分别等于[3,4,8,10,10],[12]和[12,10,20]。我以前已经将逗号分隔的整数解析为数组,但如何解析这个数组呢。不幸的是,我拥有的数据可以在数组中间有换行符,否则我会使用“getline”函数(在将文件读入字符串时)而忽略括号

似乎我需要首先将每个数组放入由括号分隔的字符串中,然后通过逗号分隔将每个数组解析为一个整数数组。这样行吗


如果是这样,我如何将括号中的字符串拆分为以前未知数量的其他字符串

如果您已经将整个内容读入一个字符串,那么以下操作应该可以:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string test = "[3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20]";
  size_t start = 0;  // first position in the string

  // find the first occurance of "]"
  size_t pos = test.find("]");

  while ( pos != string::npos ) {
    // move to position after "]"
    // so it is included in substring
    pos += 1;

    // create a substring
    string subStr = test.substr(start, pos-start);

    // remove newlines from new string
    size_t newLinePos = subStr.find("\n");
    while ( newLinePos != string::npos ) {
      subStr.erase(newLinePos,1);
      newLinePos = subStr.find("\n");
    }

   // here is the substring, like: [12, 10, 20]
    cout << "Substring: " << subStr << endl;

    // update start position for next substring
    start = pos;
    // find next occurrance of "]"
    pos = test.find("]", pos);
  }

}
#包括
#包括
使用名称空间std;
int main(){
字符串测试=“[3,4,8,10,10]\n[12]\n[12,10,n 20]”;
size\u t start=0;//字符串中的第一个位置
//查找第一次出现的“]”
大小\u t pos=测试。查找(“]”);
while(pos!=字符串::npos){
//移动到“]”之后的位置
//所以它包含在子字符串中
pos+=1;
//创建一个子字符串
string subStr=test.subStr(start,pos start);
//从新字符串中删除换行符
size\u t newLinePos=subStr.find(“\n”);
while(newLinePos!=string::npos){
子序列擦除(换行位置,1);
newLinePos=subStr.find(“\n”);
}
//下面是子字符串,如:[12,10,20]

cout如果您已经将整个内容读入一个字符串,那么以下操作应该有效:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string test = "[3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20]";
  size_t start = 0;  // first position in the string

  // find the first occurance of "]"
  size_t pos = test.find("]");

  while ( pos != string::npos ) {
    // move to position after "]"
    // so it is included in substring
    pos += 1;

    // create a substring
    string subStr = test.substr(start, pos-start);

    // remove newlines from new string
    size_t newLinePos = subStr.find("\n");
    while ( newLinePos != string::npos ) {
      subStr.erase(newLinePos,1);
      newLinePos = subStr.find("\n");
    }

   // here is the substring, like: [12, 10, 20]
    cout << "Substring: " << subStr << endl;

    // update start position for next substring
    start = pos;
    // find next occurrance of "]"
    pos = test.find("]", pos);
  }

}
#包括
#包括
使用名称空间std;
int main(){
字符串测试=“[3,4,8,10,10]\n[12]\n[12,10,n 20]”;
size\u t start=0;//字符串中的第一个位置
//查找第一次出现的“]”
大小\u t pos=测试。查找(“]”);
while(pos!=字符串::npos){
//移动到“]”之后的位置
//所以它包含在子字符串中
pos+=1;
//创建一个子字符串
string subStr=test.subStr(start,pos start);
//从新字符串中删除换行符
size\u t newLinePos=subStr.find(“\n”);
while(newLinePos!=string::npos){
子序列擦除(换行位置,1);
newLinePos=subStr.find(“\n”);
}
//下面是子字符串,如:[12,10,20]

cout您可以使用streams和
std::getline()
进行此操作,因为
std::getline()
将分隔符作为参数:

int main()
{
    std::string test = "[3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20]";

    // make data a stream (could be a std::ifstream)
    std::istringstream iss(test);

    // working vars
    std::string skip, item;

    // between square braces
    // skip to the opening '[' then getline item to the closing ']'
    while(std::getline(std::getline(iss, skip, '['), item, ']'))
    {
        // item = "3, 4, 8, 10, 10"

        // store numbers in a vector (not array)
        std::vector<int> v;

        // convert item to a stream
        std::istringstream iss(item);

        // separated by commas
        while(std::getline(iss, item, ','))
            v.push_back(std::stoi(item));

        // display the results
        std::cout << "list:" << '\n';
        for(auto i: v)
            std::cout << "\t" << i << '\n';
    }
}

您可以使用streams和
std::getline()
进行此操作,因为
std::getline()
将分隔符作为参数:

int main()
{
    std::string test = "[3, 4, 8, 10, 10]\n[12]\n[12, 10,\n 20]";

    // make data a stream (could be a std::ifstream)
    std::istringstream iss(test);

    // working vars
    std::string skip, item;

    // between square braces
    // skip to the opening '[' then getline item to the closing ']'
    while(std::getline(std::getline(iss, skip, '['), item, ']'))
    {
        // item = "3, 4, 8, 10, 10"

        // store numbers in a vector (not array)
        std::vector<int> v;

        // convert item to a stream
        std::istringstream iss(item);

        // separated by commas
        while(std::getline(iss, item, ','))
            v.push_back(std::stoi(item));

        // display the results
        std::cout << "list:" << '\n';
        for(auto i: v)
            std::cout << "\t" << i << '\n';
    }
}

一种方法是使用explode()函数。explode()的实现将根据给定的分隔符将一个字符串拆分为多个字符串。这不是最有效的方法,但它可以提供很多直观的含义

见:

一种方法是使用explode()函数。explode()的实现将根据给定的分隔符将一个字符串拆分为多个字符串。这不是最有效的方法,但它具有很大的直观意义

见:

如果他可以,也许你也可以……我在这里发布的一个实现会更嘈杂,而不仅仅是回答问题。在我看来,在这种情况下,他最好选择一个实现,而不是我为他选择。explode()是一个非特定的概念,可以根据他的个人需求采取多个方向。你没有建议实施它,只是为了通过搜索引擎找到一些东西。无论如何,无论如何。虽然搜索词足以让OP解决问题,但我同意这不是典型的SO ettiquette。因此我链接到了一个相关问题相反。如果他可以,也许你也可以……我在这里发布的一个实现会更嘈杂,而不仅仅是回答问题。在我看来,在这种情况下,他最好选择一个实现,而不是我为他选择。explode()是一个非特定的概念,可以根据他的个人需求采取多个方向。你没有建议实施它,只是为了通过搜索引擎找到一些东西。无论如何,无论如何。虽然搜索词足以让OP解决问题,但我同意这不是典型的SO ettiquette。因此我链接到了一个相关问题相反