我们可以创建临时的传入' std::vector '参数吗?

时间:2022-12-05 18:51:47
void PrintNow(const std::vector<int> &v)
{
    std::cout << v[0] << std::endl;
}

std::vector<int>().push_back(20); // this line generates no complains
PrintNow(std::vector<int>().push_back(20)); // error

From VS2010 Sp1:

从VS2010 Sp1:

eror C2664: 'PrintNow' : cannot convert parameter 1 from 'void' to 'const std::vector<_Ty> &'

eror C2664: 'PrintNow':无法将参数1从'void'转换为'const std::vector<_Ty> &'

Q> Is it possible that we can pass a temporary vector to function?

Q>是否有可能通过一个临时的向量来运行?

4 个解决方案

#1


11  

In C++11 you can just do:

在c++ 11中,你可以做到:

void PrintNow(const std::vector<int> &v)
{
    std::cout << v[0] << std::endl;
}

PrintNow({20});

VS2010 doesn't yet support this part of C++11 though. (gcc 4.4 and clang 3.1 do)

不过,VS2010还不支持c++ 11的这一部分。(gcc 4.4和clang 3.1 do)

If you only need a single element then in C++03 you can do:

如果您只需要一个元素,那么在c++ 03中,您可以做到:

PrintNow(std::vector<int>(1,20));

If you need more than one element then I don't think there's any one line solution. You could do this:

如果您需要多个元素,那么我认为不存在任何单行解。你可以这样做:

{ // introduce scope to limit array lifetime
    int arr[] = {20,1,2,3};
    PrintNow(std::vector<int>(arr,arr+sizeof(arr)/sizeof(*arr));
}

Or you could write a varargs function that takes a list of ints and returns a vector. Unless you use this a lot though I don't know that it's worth it.

或者您可以编写一个varargs函数,它获取一个ints列表并返回一个向量。除非你经常用这个,虽然我不知道它是否值得。

#2


5  

The error is generated because the return type of std::vector::push_back function is void:

由于std:::vector::push_back函数的返回类型为void,所以会产生错误:

void push_back ( const T& x );

Try the following:

试试以下:

PrintNow(std::vector<int>(1, 20));

The code above uses one of the available constructors of std::vector class:

上面的代码使用了std::vector类的一个可用构造函数:

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

#3


4  

The problem is that std::vector::push_back() returns void, not that you can't pass a temporary to the function.

问题是std:::vector: push_back()返回void,而不是说不能向函数传递临时值。

#4


3  

If all the elements are of the same value, you have one constructor that fits your needs:

如果所有元素都具有相同的值,则有一个构造函数可以满足您的需要:

PrintNow(std::vector<int>(1,20));

#1


11  

In C++11 you can just do:

在c++ 11中,你可以做到:

void PrintNow(const std::vector<int> &v)
{
    std::cout << v[0] << std::endl;
}

PrintNow({20});

VS2010 doesn't yet support this part of C++11 though. (gcc 4.4 and clang 3.1 do)

不过,VS2010还不支持c++ 11的这一部分。(gcc 4.4和clang 3.1 do)

If you only need a single element then in C++03 you can do:

如果您只需要一个元素,那么在c++ 03中,您可以做到:

PrintNow(std::vector<int>(1,20));

If you need more than one element then I don't think there's any one line solution. You could do this:

如果您需要多个元素,那么我认为不存在任何单行解。你可以这样做:

{ // introduce scope to limit array lifetime
    int arr[] = {20,1,2,3};
    PrintNow(std::vector<int>(arr,arr+sizeof(arr)/sizeof(*arr));
}

Or you could write a varargs function that takes a list of ints and returns a vector. Unless you use this a lot though I don't know that it's worth it.

或者您可以编写一个varargs函数,它获取一个ints列表并返回一个向量。除非你经常用这个,虽然我不知道它是否值得。

#2


5  

The error is generated because the return type of std::vector::push_back function is void:

由于std:::vector::push_back函数的返回类型为void,所以会产生错误:

void push_back ( const T& x );

Try the following:

试试以下:

PrintNow(std::vector<int>(1, 20));

The code above uses one of the available constructors of std::vector class:

上面的代码使用了std::vector类的一个可用构造函数:

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

#3


4  

The problem is that std::vector::push_back() returns void, not that you can't pass a temporary to the function.

问题是std:::vector: push_back()返回void,而不是说不能向函数传递临时值。

#4


3  

If all the elements are of the same value, you have one constructor that fits your needs:

如果所有元素都具有相同的值,则有一个构造函数可以满足您的需要:

PrintNow(std::vector<int>(1,20));