将字符串推入多维向量c ++

时间:2016-04-06 12:23:13

标签: c++ string vector

我正在尝试用数字2分隔字符串并将我得到的子字符推入2d向量,问题是每当我尝试时,当我尝试推送第二行时出现分段错误向量中的字符。在尝试了一些事情后,我认为问题在于我清除了矢量内容后的矢量“some”。在我看来,在清算之后,我不再能够将值推入向量中。我希望有人有任何建议,因为我被困住了。

std::string str = "11121112111";
std::vector<int> some;
std::vector<std::vector<int> > somemore;
for (unsigned int i = 0; i < str.length(); i++)
{
    if (str[i] == '2')
    {
        somemore.push_back(some);
        some.clear();
    }
    else
    {
        some.push_back(1);
    }
}
for (unsigned int i = 0; i <= 3; i++)
{
    for (unsigned int j = 0; j <= 3; j++)
    {
        std::cout << somemore[i][j] << std::endl;
    }
}

4 个答案:

答案 0 :(得分:0)

我会改变最后一部分:

.factory('myHttpInterceptor',['$q','$location',function($q,$location) {
    return {
            responseError: function(response){
            console.log(response.status+' intercepted');
            return $q.reject(response);
           }
         };
     }]);

进入这个:

for(unsigned int i = 0; i <=3; i++)
{
   for(unsigned int j = 0; j <=3; j++)
      {
          std::cout << somemore[i][j] << std::endl;
      }
}

它更安全。

答案 1 :(得分:0)

在这个循环中

for(unsigned int i = 0; i < str.length(); i++)
{ 
  if(str[i] == '2')
  {
      sommore.push_back(som);
      som.clear();
  }
  else
  {
      som.push_back(1);
  }
}

不清楚som是否是声明为

的向量
std::vector<int> some;
                 ^^^^^

字符串的最后一部分

std::string str = "11121112111";
                           ^^^
向量sommore忽略

。因此,向量只包含两个元素,对应于字符串中的两个2​​(s)。

结果这些循环

   for(unsigned int i = 0; i <=3; i++)
   {
       for(unsigned int j = 0; j <=3; j++)
       {
           std::cout << sommore[i][j] << std::endl;
       }
   }
使用幻数3

具有未定义的行为。

即使在这种情况下,向量及其子向量包含3个元素,条件i <=3j <=3也是错误的。

您可以采用此演示程序中显示的以下方法

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

int main()
{
    std::vector<std::vector<int>> v;
    std::string str = "11121112111";

    for ( std::string::size_type pos = 0; ( pos = str.find_first_not_of( "2", pos ) ) != std::string::npos; )
    {
        auto n = str.find( "2", pos );
        if ( n == std::string::npos ) n = str.size();
        v.push_back( std::vector<int>( n - pos, 1 ) );
        pos = n;
    }

    for ( const auto &row : v )
    {
        for ( int x : row ) std::cout << x;
        std::cout << std::endl;
    }        

}    

程序输出

111
111
111

答案 2 :(得分:0)

使用c ++ 11和Boost,您可以制作更优雅的解决方案,而无需使用递增索引的循环。

#include <vector>
#include <string>
#include <iostream>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

int main()
{ 
    std::string str = "11121112111";
    std::vector<std::string> string_vector;
    boost::split(string_vector, str, boost::is_any_of("2"));

    std::vector<std::vector<int>> int_vector;

    for (auto& s : string_vector)
        int_vector.push_back(std::vector<int>(s.size(), 1));

    for (auto& v : int_vector)
        for (auto& i : v)
            std::cout << i << std::endl;

    return 0;
}

答案 3 :(得分:0)

正如我在评论中已经提到的,您的代码中存在两个问题:

  1. 您没有将最后some推到somemore,因为2末尾没有str
  2. 您的上一个循环过大 - 您有一个3x3矩阵,但是从0到3,您期望4x4
  3. 顺便说一句,因为你只计算一个,所以你不需要some

    std::string str = "11121112111";
    std::vector<std::vector<int>> somemore;
    size_t count = 0;
    for (size_t = 0; i < str.length(); i++) {
        if (str[i] == '2') {
            somemore.push_back(std::vector<int>(count, 1));
            count = 0;
        }
        else {
            ++count;
        }
    }
    for (size_t i = 0; i < somemore.size(); ++i) {
        for (size_t j = 0; j < somemore[i].size(); ++j) {
            std::cout << somemore[i][j] << std::endl;
        }
    }
    

    您也可以使用迭代器替换最后两个循环,或者如果您有c++11可用:

    for (const auto &s: somemore) {
        for (const auto &v: s) {
            std::cout << v << std::endl;
        }
    }