【问题标题】:The element of vector not updating向量的元素不更新
【发布时间】:2021-03-09 04:51:40
【问题描述】:
//simply print each elements in a vector
template<class Iterp>
void print_iter(Iterp begin, Iterp end, std::string delim=", ", std::string ending="\n"){
    int len = (end-begin);
    for (size_t i = 0; i < len-1; i++)
    {
        std::cout << (*(begin+i)) << delim;
    }
    if (len>1){
        std::cout << (*(end-1));
    }
    std::cout << ending;
}    

template<class A, class B>
void pr(A first, A end, std::vector<std::vector<B>> &the_result){
    int len = end-first;
    for (size_t a = 0; a < len; a++)
    {
        for (size_t b = 0; b < len; b++)
        {
            for (size_t c = 0; c < len; c++)
            {
                //theres a bunch ways i tried to save these in "the_result" variable, and I ended up with this.
                std::vector<B> result(first, end);
                result.at(a) = (*(first+a));
                result.at(b) = (*(first+b));
                result.at(c) = (*(first+c));
                
                print_iter(result.begin(), result.end());
                //the problem is the "result" variable is not updating using .at() function. So the "result" variable content is still 1, 2, 3.
                

                the_result.push_back(result); //this is not the problem since the "result" variable is still 1, 2, 3

                // std::cout << (*(first+a)) << " " << (*(first+b)) << " " << (*(first+c))
                // But, this one shows it's producing the right permutations.
            }
            
        }
                
    }
    
}
    int main(){
        std::vector<int> test{1, 2, 3}; //tried to create the permutations of this
        int n = 27; //3^3 = 27

        std::vector<std::vector<int>> result(n, std::vector<int>(test.begin(), test.end()));
/*hoping to save all the permutations in "result" variable, each as a vector. ex:
    123
    113
    111
    ...

*/
        pr(test.begin(), test.end(), result);
    }

所以我尝试创建一个函数来产生重复元素的排列,并希望将每个排列保存在二维向量中。 但是,我什至无法使用 .at() 函数指针元素更新“结果”变量向量。但是当我手动打印时,它显示它是正确的答案。

我还探索了其他一些存在类似问题的 StackOverflow,但没有适合我的答案。 vector element not updating

Why is my elements in my vector of objects not updating upon calling one of the objects member function?

Dereference vector pointer to access element //尝试使用指针但还是不行

我在哪里做错了?

【问题讨论】:

  • 请附上minimal reproducible example。您发布的只是一个模板。它不是函数,我们不知道你如何调用函数
  • @idclev463035818 哦,是的,对不起,我忘了
  • 你永远不会在函数中更新the_result
  • @TedLyngmo 连_result 变量都没有,核心问题是pr() 中的“result”变量没有更新
  • 您发布的代码不会产生任何输出。你在使用调试器吗?在这种情况下,您需要具体说明您在程序中的哪个点观察到的值

标签: c++ stdvector


【解决方案1】:

你的逻辑有缺陷:

   result.at(a) = (*(first+a));
   result.at(b) = (*(first+b));
   result.at(c) = (*(first+c));

无论abc 的值是什么,您在每次迭代中为相同的元素分配相同的数字。它们与您用于初始化 result 的元素相同。

对于3 元素,您宁愿需要以下内容:

   result[1] = (*(first+a));
   result[2] = (*(first+b));
   result[3] = (*(first+c));

【讨论】:

  • 不幸的是,std::next_permutation 没有产生重复的符号(例如:111、112、...)。但是,您的示例是使用常量值设置结果。我怎样才能让它动态地处理任意长度的向量?
  • @KevinAusto 因为那不是排列 ;)
  • @KevinAgusto 对不起,我错过了你实际上写的“重复排列”我会删除那部分
  • 是的,这就是为什么我尝试创建自己的函数,但没有递归。
  • @KevinAgusto 请为此提出一个新问题。这里的问题是“为什么向量元素没有更新?”这就是我试图回答的问题
猜你喜欢
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2011-02-07
  • 2021-10-12
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多