简体   繁体   English

C ++:将指针向量传递给函数

[英]c++: passing vector of pointers into a function

In a program I am writing I have a vector of pointers to try and save memory usage and although this will make my program more efficient I am having trouble passing the vector of pointers into the function direct(). 在我正在编写的程序中,我有一个指针向量来尝试节省内存,尽管这将使我的程序更高效,但在将指针向量传递到函数direct()中时遇到了麻烦。 Any help with the correct syntax for passing this into the function is greatly appreciated 非常感谢将其传递给函数的正确语法的任何帮助

The current error being shown is : "error cannot convert 'std::vector*> ' to 'const string '... for argument '1'... The line this error is being flagged on is the line in which the function direct is called 当前显示的错误是:“错误无法将'std :: vector *> '转换为'const string '...,参数'1'...标记此错误的行是该函数所在的行直接称为

#include <iostream>
#include <vector>

using namespace std;

// a function used to display an array used for testing purposes
void display_array(const string *arr, size_t size )
{
    int i;
    for (i = 0; i < size; i++){
       cout<<(int(arr[i][0]))-64;
       cout<<(int(arr[i][1]))-64;
       cout<<",";
    }
}
// Takes in the connections to the start and the connections to the end and returns the connection if
//there is a direct connection else returns 0
string direct(const string *destination, char *start, size_t destination_size) {
    for (int i = 0; i<destination_size;i++)
        if ((&destination[i][0] == start) or (&destination[i][1] == start))
            return destination[i];
}

int main()
{
    string current;
    std::vector<string> paths;
    std::vector<string*> start_connections;
    std::vector<string*> destination_connections;
    char start;
    char destination;

    cout<<"Input paths in the form 'AB'(0 to exit)\n";
    cin>>current;
    while (current != "0"){
        paths.push_back(current);
        cin>>current;
    }

    cout<<"Input starting location\n";
    cin>> start;
    cout<<"Input final destination\n";
    cin>>destination;
    for(int i = 0; i < paths.size(); i++) {
        if ((paths[i][0] == destination) or (paths[i][1] == destination))  //all connections to the destination
            destination_connections.push_back(&paths[i]); // paths stored as a pointer to paths array
        if ((paths[i][0] == start) or (paths [i][1] == start)) //all connections to the start
            start_connections.push_back(&paths[i]); // paths stored as a pointer to paths array
    }
    cout<<direct(&destination_connections,&start,destination_connections.size());

    if( !paths.empty() )
      display_array( &paths[0], paths.size() );
}

The compiler's telling you exactly what's wrong - a vector is not a pointer. 编译器会告诉您确切的问题-向量不是指针。

Ideally, you shouldn't be using pointers at all - declare your vectors as 理想情况下,您根本不应该使用指针-将向量声明为

std::vector<std::string>

and pass a reference to the function using it 并使用它传递对函数的引用

... direct(const std::vector<std::string> & dest, ...)

You then just pass the vector as if by value, but the reference operator tells the compiler to just pass its address instead of the whole object. 然后,您只需按值传递向量即可,但是引用运算符告诉编译器仅传递其地址而不是传递整个对象。

You also get the benefit of not having to pass its size separately, as the function iterating through it can access that directly (although accessing it by index isn't really the OO way). 您还获得了不必分别传递其大小的好处,因为遍历它的函数可以直接访问它的大小(尽管按索引访问它并不是真正的OO方法)。

In C++, if you're using a naked pointer, you're probably doing it wrong ;) 在C ++中,如果使用的是裸指针,则可能做错了;)

You are trying to pass a vector<string*>* where a string* is expected. 您正在尝试传递vector<string*>*期望为string*位置。

Change this: 更改此:

direct(&destination_connections, &start, destination_connections.size());

To this: 对此:

direct(&destination_connections[0], &start, destination_connections.size());

Or this, if you are using C++11: 或者,如果您使用的是C ++ 11:

direct(destination_connections.data(), &start, destination_connections.size());

That being said, or is not a valid C++ keyword, you need to use || 话虽如此, or它不是有效的C ++关键字,您需要使用|| instead. 代替。 And I think you are mishandling pointers inside of display() . 而且我认为您在display()内部处理了指针。 You need to do a code review of what you are really trying to accomplish. 您需要对您真正要完成的工作进行代码审查。

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

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