【问题标题】:Pushing values into struct将值推入结构
【发布时间】:2013-07-05 23:23:54
【问题描述】:

为标题道歉,我正在努力想出一个。

我在Here 上问了一个问题,我正在尝试改进它。我有一个结构:

namespace Complex {

typedef struct complex_t { 
    double re; 
    double im;
    } complex;
}

我希望将array 或vector 推回到复数的一维向量,如下所示:

template<typename Iterator>
vector<Complex::complex> convertToComplex(Iterator begin, Iterator end)
{
     vector<Complex::complex> vals12(10);


     for(auto i=begin; (i != end); i++)
     {
      vals12[i].re = *i;
     }

     return vals12;
  } 

并得到以下错误:

no viable overloaded operator[] for type 'vector<Complex::complex>' vals12[i].re = *i;

现在请注意我是否执行以下操作:

vals12[0].re = 10;

会起作用,但是,当使用数组时,它会抛出这个错误,我不明白为什么会这样。我唯一的想法是,这可能是因为我通过 array 而不是向量。完整代码如下:

#include <iostream>
#include <vector>
#include "complex.h"

using namespace std;
//using namespace Complex; 

template<typename Iterator>
vector<Complex::complex> convertToComplex(Iterator begin, Iterator end)
{
     vector<Complex::complex> vals12(10);


 for(auto i=begin; (i != end); i++)
 {
     vals12[i].re = *i;
 }

 return vals12;
 }

 class Value
 {
     public:
     template <typename Fred>
     Value(Fred begin, Fred end)
     {
          vector<Complex::complex> vec = convertToComplex(begin, end);
     }
  };

  int main()
  { 
  double vals[] = {10, 23, 23, 34, 354};
  Value v(std::begin(vals), std::end(vals));
  }

有没有人知道我哪里出错了?我真的希望有人可以帮助我。

更新:

template<typename Iterator>
vector<Complex::complex> convertToComplex(Iterator begin, Iterator end)
{
vector<Complex::complex> vals12(10);

size_t index = 0;

for(auto i=begin; (i != end); i++)
{
    complex c;
    c.re = *i;
    c.im = *i;

    vals12[index].re = c.re;
    vals12[index].im = c.im;

    index++;
}

return vals12;
 }

错误:

error: assigning to 'double *' from incompatible type 'double'; remove * c.re = *i;

【问题讨论】:

  • C++ 不是 C,你可以说 struct complex { ... }; 并失去 typedef
  • 哪一行代码在您的更新中给出了错误?

标签: c++ oop templates iterator


【解决方案1】:

当您在for 初始化程序中将i 声明为auto 时,它采用begin 的类型,在本例中是指向double 的指针。然后,您尝试将该指针传递给std::vector 的索引运算符(尽管间接通过[i])。

您将需要使用一个额外的变量来充当索引。

size_t index = 0;
for(auto i=begin; (i != end); i++)
{
    vals12[index++].re = *i;
}

【讨论】:

  • 感谢您的回复。使用这种方法,我得到以下错误:assigning to 'double *' from incompatible type 'double'; remove * vals12[index++].re = *i;
  • 使用您问题中提供的代码和我回答中的示例,它可以编译并运行良好。
【解决方案2】:

其他答案已经解释了您的代码无法编译的原因。你实际上想要一个变换算法:

namespace Complex {
    struct complex_t { 
    double re; 
    double im;
  };
}
template<typename Iterator>
vector<Complex::complex_t> convertToComplex(Iterator begin, Iterator end)
{
     vector<Complex::complex_t> vals12;

     std::transform(begin, end, std::back_inserter(vals12), 
                   [](const double& d ){ return Complex::complex_t{d,0}; });

     return vals12;
 }

【讨论】:

    【解决方案3】:

    请注意,i 是 Iterator。但是,operator[] 期望 int 用于索引到 vector。您至少有两种解决方案:

    1. 为向量中的索引创建一个计数器。

    2. 创建一个空向量并使用push_back()。

    【讨论】:

    • 嘿,我实施了您提出的建议,但出现错误(请参阅我的原始帖子)。感谢您的帮助
    • vector&lt;Complex::complex&gt; vals12(10); 生成一个包含 10 个默认构造元素的向量。与这个答案所说的相反,元素 是 可供使用,并且 vals12[0].re = *i; 定义明确。
    • @Mankarse grr 这就是我来回切换语言的结果(我最初的答案是 Java 的 ArrayList 的行为。)...感谢您的更正。
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多