简体   繁体   English

为向量分配一个元素向量

[英]allocate an element vector to vector

This is the segment function I write:这是我写的段函数:

vector<int> v1(const vector<int> &v2, const vector<int> &v3) {

  int v2_index = 0;
  int v3_index = 0;
  int v1_INDEX = 0;

  for(int i=0; i < v3.size(); ++i) {
    if(v2[v2_INDEX] == v3[v3_INDEX]) {
       int  x= v2[v2_INDEX];
       v1[v1_INDEX] = x;
       ++v1_INDEX;
     }
     if (v2[0] != v3[0]) {
       v2_INDEX++;
     }
     v3_INDEX++;
   }
}

I must use vector as a function.我必须使用向量作为函数。 If the v2 element equal to v3 element, I want to allocate one element (not duplicate; push_back or v2[0]= v1[0]) to the v1 vector:如果 v2 元素等于 v3 元素,我想为 v1 向量分配一个元素(不重复;push_back 或 v2[0]= v1[0]):

I've tried:我试过了:

v1.push_back(v2.push_back(i));
v1[v1_INDEX] = v2[v2_index];
int x = v2[v2_index]; v1.push_back(x);
v1[v2[v2_index]];

All of them do not compile.他们都没有编译。 Why can I allocate an element v2[i] to -----> v1[i] correctly without using extra library?为什么我可以在不使用额外库的情况下将元素 v2[i] 正确分配给 -----> v1[i]?

I get the error below:我收到以下错误:

error: request for member ‘push_back’ in ‘v1’, which is of non-class type 

The crux of the matter is that in C++, unlike Pascal , a function returns values by using a return statement.问题的关键在于,在 C++ 中,与Pascal不同,函数通过使用return语句返回值。 In C++ it is impossible to access the returned object through function name.在 C++ 中,不可能通过函数名访问返回的对象。 In your code:在您的代码中:

   v1[v1_index] = x;

The v1 refers to the function itself, not the returned object. v1指的是函数本身,而不是返回的对象。 So, the code tries to access function v1 is if it were an array, or a vector.因此,代码尝试访问函数v1是它是数组还是向量。 Which makes no sense, and hence the error:这是没有意义的,因此错误:

<source>: In function 'std::vector<int> v1(const std::vector<int>&, const std::vector<int>&)':
<source>:12:19: warning: pointer to a function used in arithmetic [-Wpointer-arith]
        v1[v1_index] = x;
                   ^

To get the functionality you want, it is up to you to define the returned object, and return it at the end:要获得您想要的功能,您需要定义返回的对象,并在最后返回它:

vector<int> v1(const vector<int> &v2, const vector<int> &v3) {

  int v2_index = 0;
  int v3_index = 0;
  int v1_index = 0;

  vector<int> ret;
  for(int i=0; i < v3.size(); ++i) {
    if(v2[v2_index] == v3[v3_index]) {
       int  x= v2[v2_index];

       // Bug: out of bounds
       ret[v1_index] = x;
       ++v1_index;
     }
     if (v2[0] != v3[0]) {
       v2_index++;
     }
     v3_index++;
   }
   return ret;
}

This compiles, but you still have a critical bug.这可以编译,但您仍然有一个严重的错误。 The bug is accessing ret out of bounds.该错误正在访问ret越界。 A better solution is to ditch v1_index and simply call push_back() instead:更好的解决方案是v1_index并简单地调用push_back()代替:

   int  x= v2[v2_index];
   ret.push_back(x);

Even better, use a range-for loop instead of all the mess with v3_index variables.更好的是,使用 range-for 循环代替v3_index变量的所有混乱。 It is as simple as:它很简单:

for (auto v3_element: v3) {
  ... your code goes here...
}

No need to maintain indexes, and no need to access v3[...] .无需维护索引,也无需访问v3[...] This is all done for you by for .这一切都由for为您完成。 Unfortunately, you can't get rid of v2_index due to the way it is incremented, but other index variables are not needed.不幸的是,由于v2_index递增的方式,您无法摆脱它,但不需要其他索引变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM