将向量传递给函数 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7677007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:20:25  来源:igfitidea点击:

passing vector to function c++

c++vectorparameter-passing

提问by TT12

I have a main.cpp test.h and test.cpp> I am trying to pass my vector through so i can use it in test.cpp but i keep getting errors.

我有一个 main.cpp test.h 和 test.cpp> 我试图通过我的向量,所以我可以在 test.cpp 中使用它,但我不断收到错误。

   //file: main.cpp
    int main(){
        vector <Item *> s;
         //loading my file and assign s[i]->name and s[i]-address
         tester(s);
    }

    //file: test.h
    #ifndef TEST_H
    #define TEST_H
    struct Item{
        string name;
        string address;
    };
    #endif

    //file: test.cpp
    int tester(Item *s[]){
        for (i=0; i<s.sizeof();i++){
            cout<< s[i]->name<<"  "<< s[i]->address<<endl;
        }
        return 0;
    }



    ---------------errors--------
    In file included from main.cpp:13:
    test.h:5: error: astringa does not name a type
    test.h:6: error: astringa does not name a type
    main.cpp: In function aint main()a:
    main.cpp:28: error: cannot convert astd::vector<Item*, std::allocator<Item*> >a to aItem**a for argument a1a to aint tester(Item**)a

回答by Chad

A std::vector<T>and T* []are not compatible types.

Astd::vector<T>T* []是不兼容的类型。

Change your tester()function signature as follows:

更改您的tester()函数签名如下:

//file: test.cpp
int tester(const std::vector<Item>& s)   // take a const-reference to the std::vector
                                         // since you don't need to change the values 
                                         // in this function
{
    for (size_t i = 0; i < s.size(); ++i){
        cout<< s[i]->name<<"  "<< s[i]->address<<endl;
    }
    return 0;
}

There are several ways you could pass this std::vector<T>and all have slightly different meanings:

有几种方法可以传递它,std::vector<T>并且它们的含义都略有不同:

// This would create a COPY of the vector
// that would be local to this function's scope
void tester(std::vector<Item*>); 

// This would use a reference to the vector
// this reference could be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(std::vector<Item*>&);

// This would use a const-reference to the vector
// this reference could NOT be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(const std::vector<Item*>&);

// This would use a pointer to the vector
// This does NOT involve a second copy of the vector
// caveat:  use of raw pointers can be dangerous and 
// should be avoided for non-trivial cases if possible
void tester(std::vector<Item*>*);

回答by NPE

  1. You should #include <string>.
  2. string nameshould read std::string nameetc. Same goes for std::vector.
  3. You're calling tester()with a vector, yet it expects an array (the two are not interchangeable).
  4. s.sizeof()is incorrect for both an array and a vector; for the latter, use s.size()or, better yet, use an iterator.
  1. 你应该#include <string>
  2. string name应该阅读std::string name等。同样适用于std::vector
  3. 您正在tester()使用 a 进行调用vector,但它需要一个数组(两者不可互换)。
  4. s.sizeof()对数组和向量都不正确;对于后者,使用s.size()或者更好的是使用迭代器。

These are just the errors that immediately jump out; there may be more.

这些只是立即跳出的错误;可能还有更多。

回答by Griwes

Pass it as std::vector<Item *> &(reference to vector) and use iterator to iterate through it.

将其作为std::vector<Item *> &(对向量的引用)传递并使用迭代器对其进行迭代。

回答by crashmstr

A vectoris not an array.

Avector不是数组。

int tester(vector<Item *> &s)

(pass as a reference to avoid copying or if you need to modify)

(作为参考传递以避免复制或需要修改)

You also need to modify your code inside the testerfunction to work correctly as a vector.

您还需要修改tester函数内的代码才能作为向量正常工作。

回答by Philipp

You should fix

你应该修

test.h:5: error: astringa does not name a type

first, probably by using namespace std;and #include <string>

首先,可能由using namespace std;#include <string>

回答by bbtrb

You are missing includes

你缺少的包括

#include <string>
#include <vector>

and you need to use std::stringand std::vector<>. A std::vectoris not an array, so you should pass the vector as reference

并且您需要使用std::stringstd::vector<>。Astd::vector不是数组,因此您应该将向量作为参考传递

int tester(std::vector<Item*> & vec) { //... }

or even as const std::vector<Item*> &if you are not going to modify the passed vector.

甚至就const std::vector<Item*> &好像您不打算修改传递的向量一样。

Also, are you sure, that you'll need a vector of pointers? What are you trying to achieve?

另外,您确定需要一个指针向量吗?你想达到什么目的?