Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:是否可以生成多类型函数参数?_C++_Function_Parameters - Fatal编程技术网

C++ C++;:是否可以生成多类型函数参数?

C++ C++;:是否可以生成多类型函数参数?,c++,function,parameters,C++,Function,Parameters,有没有可能使一个函数fun包含一个参数:数组或向量? 例如,如果我这样做,它将起作用: 我定义了fun(参数)函数参数可以是std::vector类型或int[]类型。我想把向量/数组中的所有元素加起来 它的工作原理类似于python函数参数(def=函数decellation,stuff in()是参数列表,不带特定类型,print==#include或stdio.h中的“printf(“something”): def函数(param1,param2):#param1和param2 打印(p

有没有可能使一个函数
fun
包含一个参数:数组或向量? 例如,如果我这样做,它将起作用:

我定义了
fun(参数)
函数<代码>参数可以是
std::vector
类型或
int[]
类型。我想把向量/数组中的所有元素加起来

它的工作原理类似于python函数参数(def=函数decellation,stuff in()是参数列表,不带特定类型,print==#include或stdio.h中的“printf(“something”):

def函数(param1,param2):#param1和param2
打印(param1+param2)#可以是具有+运算符的任何类型
#它们是float、int、str(string)或者更多
< C++ >

这是可能的吗?
谢谢您,CppPythonDude。

您必须使用相同的名称但不同的参数重写函数。这称为函数重载,请查看

我定义了
fun(参数)
函数。参数可以是
std::vector
类型或
int*
类型。我想把向量/数组中的所有元素加起来


您如何知道只指向
int
的指针的元素数?通常的方法是传递一对迭代器

你能给我看一些示例代码吗?我从来没有用过

将迭代器传递给第一个元素,将迭代器传递给要迭代的范围的最后一个元素。在区间术语中,它是半开的
[第一个,最后一个)

#包括

类型作为前向迭代器的要求是,它支持操作
iterator++
*iterator++
。普通指针也可以被视为前向迭代器,因为这些操作对指针是有效的。事实上,指针更强大,可以用作随机访问迭代器


<> P>但是,要解决你的问题:除了为不同的参数重载一个函数或者写一个函数模板,还有一个原因是C++ 17。

不需要重新创建轮子:C++标准库有一个确切的功能:它使用两个迭代器参数并返回元素的和。(您还可以提供第三个参数,该参数定义您自己版本的加法运算符,允许任何类型的累加操作):

#包括
#包括
#包括
#包括
#包括
int main()
{
int foo[]{1,2,3};

std::cout通常的方法是传递一对迭代器。@πάνταῥεῖ 我正在尝试将参数设置为向量或数组。如果您传递向量或数组,它就会起作用。@cpppythonude我相信@swardfish手头有正确的解决方案。@swardfish您能给我看一些示例代码吗,我从来没有使用过成对的方法?不仅是可能的,而且可以用几种不同的方式实现:您可以编写多个重载函数-每个参数类型一个,或者您可以编写一个模板函数,或者您可以实际实现python风格的动态类型系统,并编写一个接受任何类型对象的函数。因此,我定义了fun(向量)和fun(数组)?…或者使用函数模板是的,您可以同时实现这两个。您可以让它们做相同或不同的事情,但无论哪种方式,您都必须同时编写它们。@Clifford wat dat?我是dumb@CppPythonDude:但现在我给了你搜索词,你可以用谷歌搜索;-)@CppPythonDude你应该保持活跃,并对你的帖子做出回应。在你睡觉的时候可能会发生很多事情。哈哈…你说“不要重新发明轮子”,是的,我们可以有更好的
sum
功能:
#include <array>
#include <vector>
#include <list>
#include <iostream>

template<typename ForwardIterator>
long sum(ForwardIterator first, ForwardIterator last)
{
    long sum{};
    while (first != last)
        sum += *first++;
    return sum;
}

int main()
{
    int foo[]{ 1, 2, 3 };
    std::cout << sum(std::begin(foo), std::end(foo)) << '\n';

    std::array<int, 3> bar{ 1, 2, 3 };
    std::cout << sum(bar.begin(), bar.end()) << '\n';

    std::vector<int> qux{ 1, 2, 3 };
    std::cout << sum(qux.begin(), qux.end()) << '\n';

    std::list<int> hugo{ 1, 2, 3 };
    std::cout << sum(hugo.begin(), hugo.end()) << '\n';
}
#include <array>
#include <vector>
#include <list>
#include <iostream>

template<typename ForwardIterator>
long sum(ForwardIterator first, ForwardIterator last)
{
    long sum{};
    while (first != last)
        sum += *first++;
    return sum;
}

template<typename T>
long sum(T const &container)
{
    return sum(std::begin(container), std::end(container));
}

int main()
{
    int foo[]{ 1, 2, 3 };
    // please note that the array is passed by reference (no pointer):
    std::cout << sum(foo) << '\n';

    // sum(&foo[0]);  // wouldn't work because there is
    // no overload of std::begin() or std::end() for raw pointers

    std::array<int, 3> bar{ 1, 2, 3 };
    std::cout << sum(bar) << '\n';

    std::vector<int> qux{ 1, 2, 3 };
    std::cout << sum(qux) << '\n';

    std::list<int> hugo{ 1, 2, 3 };
    std::cout << sum(hugo) << '\n';
}
#include <array>
#include <vector>
#include <list>
#include <numeric>
#include <iostream>

int main()
{
    int foo[]{ 1, 2, 3 };
    std::cout << std::accumulate(std::begin(foo), std::end(foo)) << '\n';

    std::array<int, 3> bar{ 1, 2, 3 };
    std::cout << std::accumulate(bar.begin(), bar.end()) << '\n';

    std::vector<int> qux{ 1, 2, 3 };
    std::cout << std::accumulate(qux.begin(), qux.end()) << '\n';

    std::list<int> hugo{ 1, 2, 3 };
    std::cout << std::accumulate(hugo.begin(), hugo.end()) << '\n';
}