C++字符串数组初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9626722/
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
c++ string array initialization
提问by poy
I know I can do this in C++:
我知道我可以在 C++ 中做到这一点:
string s[] = {"hi", "there"};
But is there anyway to delcare an array this way without delcaring string s[]
?
但是无论如何,有没有以这种方式 delcare 数组而不用 delcaring string s[]
?
e.g.
例如
void foo(string[] strArray){
// some code
}
string s[] = {"hi", "there"}; // Works
foo(s); // Works
foo(new string[]{"hi", "there"}); // Doesn't work
采纳答案by Xeo
In C++11 you can. A note beforehand: Don't new
the array, there's no need for that.
在 C++11 中你可以。事先注意:不要new
阵列,没有必要。
First, string[] strArray
is a syntax error, that should either be string* strArray
or string strArray[]
. And I assume that it's just for the sake of the example that you don't pass any size parameter.
首先,string[] strArray
是语法错误,应该是string* strArray
或string strArray[]
。而且我假设这只是为了示例,您不传递任何大小参数。
#include <string>
void foo(std::string* strArray, unsigned size){
// do stuff...
}
template<class T>
using alias = T;
int main(){
foo(alias<std::string[]>{"hi", "there"}, 2);
}
Note that it would be better if you didn't need to pass the array size as an extra parameter, and thankfully there is a way: Templates!
请注意,如果您不需要将数组大小作为额外参数传递会更好,幸好有一种方法:模板!
template<unsigned N>
void foo(int const (&arr)[N]){
// ...
}
Note that this will only match stack arrays, like int x[5] = ...
. Or temporary ones, created by the use of alias
above.
请注意,这只会匹配堆栈数组,例如int x[5] = ...
. 或临时的,由alias
上面的使用创建。
int main(){
foo(alias<int[]>{1, 2, 3});
}
回答by Guangchun
Prior to C++11, you cannot initialise an array using type[]. However the latest c++11 provides(unifies) the initialisation, so you can do it in this way:
在 C++11 之前,不能使用 type[] 初始化数组。然而,最新的 c++11 提供(统一)初始化,所以你可以这样做:
string* pStr = new string[3] { "hi", "there"};
See http://www2.research.att.com/~bs/C++0xFAQ.html#uniform-init
见http://www2.research.att.com/~bs/C++0xFAQ.html#uniform-init
回答by Cheers and hth. - Alf
With support for C++11 initializer lists it is very easy:
支持 C++11 初始值设定项列表很容易:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using Strings = vector<string>;
void foo( Strings const& strings )
{
for( string const& s : strings ) { cout << s << endl; }
}
auto main() -> int
{
foo( Strings{ "hi", "there" } );
}
Lacking that (e.g. for Visual C++ 10.0) you can do things like this:
缺少它(例如对于 Visual C++ 10.0),您可以执行以下操作:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef vector<string> Strings;
void foo( Strings const& strings )
{
for( auto it = begin( strings ); it != end( strings ); ++it )
{
cout << *it << endl;
}
}
template< class Elem >
vector<Elem>& r( vector<Elem>&& o ) { return o; }
template< class Elem, class Arg >
vector<Elem>& operator<<( vector<Elem>& v, Arg const& a )
{
v.push_back( a );
return v;
}
int main()
{
foo( r( Strings() ) << "hi" << "there" );
}
回答by DavidJ
In C++11 and above, you can also initialize std::vector
with an initializer list. For example:
在 C++11 及更高版本中,您还可以std::vector
使用初始化列表进行初始化。例如:
using namespace std; // for example only
for (auto s : vector<string>{"one","two","three"} )
cout << s << endl;
So, your example would become:
因此,您的示例将变为:
void foo(vector<string> strArray){
// some code
}
vector<string> s {"hi", "there"}; // Works
foo(s); // Works
foo(vector<string> {"hi", "there"}); // also works