将基于堆的N元数组引用传递给函数

时间:2019-02-04 16:05:21

标签: c++ arrays templates

我试图将N元数组传递给C ++中的函数。

简单起见,对于一维数组,这是一个:

#include <iostream>

template <size_t N> int sign(int(&arr)[N], int i)
{
    return (arr[i] >= 0) ? +1 : -1 ;
}

int main(int argn, char** argv)
{
    const int SIZE = 2;

    int (*arr1d) = new int[SIZE];
    arr1d[0] = 12;
    arr1d[1] = -1;

    std::cout << "sign 0 : " << sign(arr1d, 0) << std::endl ;     
    std::cout << "sign 1 : " << sign(arr1d, 1) << std::endl ;
}

但这不会编译:

Sign.cpp:17:50: error: no matching function for call to ‘sign(int*&, int)’
     std::cout << "sign 1 : " << sign(arr1d, 1) << std::endl ;
                                              ^
Sign.cpp:3:29: note: candidate: template<long unsigned int N> int sign(int (&)[N], int)
 template <size_t N> int sign(int(&arr)[N], int i)
                         ^~~~
Sign.cpp:3:29: note:   template argument deduction/substitution failed:
Sign.cpp:17:50: note:   mismatched types ‘int [N]’ and ‘int*’
     std::cout << "sign 1 : " << sign(arr1d, 1) << std::endl ;

我不介意使用模板(这似乎是这里的问题)。

有什么建议吗?


二维情况下的相同错误:

#include <iostream>

template <size_t M, size_t N> int sign(int(&arr)[M][N], int i, int j)
{
    return (arr[i][j] >= 0) ? +1 : -1 ;
}

int main(int argn, char** argv)
{
    const int SIZE = 2;

    int (*arr2d)[SIZE] = new int[SIZE][SIZE];                           
    arr2d[0][0] = 12;
    arr2d[0][1] = -1;
    arr2d[1][0] = 32;
    arr2d[1][1] = -4;

    std::cout << "sign 0 1: " << sign(arr2d, 0, 1) << std::endl ;
}

1 个答案:

答案 0 :(得分:1)

尝试将指向数组的指针传递给sign函数。

#include <iterator>
#include <vector>
#include<iostream>

using namespace std;

class Str
{

private:
    std::vector<char> data;

public:
    typedef std::vector<char>::size_type size_type;
    typedef std::vector<char>::iterator iterator;
    typedef std::vector<char>::const_iterator const_iterator;

    iterator begin() { return data.begin(); }
    const_iterator begin() const { return data.begin(); }
    iterator end() { return data.end(); }
    const_iterator end() const { return data.end(); }


    // default constructor must be defined explicitly, since non-default constructors are also defined
    Str() { }
    Str(const size_type n, const char c) : data(n, c) {  }
    Str(const char* cp)
    {
        std::copy(cp, cp + std::strlen(cp), std::back_inserter(data));
    }
    // Since this is a template, the iterators can be anything: pointers in an array of chars, std::vector<string> iterators, std::std::vectortor<string> iterators, std::list<string> iterators, etc.
    template <class In> Str(In b, In e)
    {
        std::copy(b, e, std::back_inserter(data));
    }
    char& operator[](size_type i) { return data[i]; }
    const char& operator[](size_type i) const { return data[i]; }
    Str& operator+=(const Str& s)
    {
        std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
        return *this;
    }
    size_type size() const
    {
        return data.size();
    }

    // conversion operators
    operator std::vector<char>() const;
    operator bool() const;

    template <class In> void insert(iterator dest, In b, In e)
    {
        data.insert(dest, b, e);
    }

};

Str operator+(const Str& s1, const Str& s2)
{
    Str s = s1;
    s += s2;
    return s;
}

Str::operator std::vector<char>() const
{
    std::vector<char> ret = data;
    return ret;

}

Str::operator bool() const
{
    if (data.size() > 0)
        return true;
    return false;
}


int main()

{

    Str name = "Joe";
    Str greeting = "Hello, " + name + "!";

    return 0;
}

然后您可以按以下方式调用sign函数:

template <size_t M, size_t N> 
int sign(int(*arr)[N], int i, int j)
{
    return (arr[i][j] >= 0) ? +1 : -1 ;
}