实现数组初始化 [英] Implementing Array Initializer
问题描述
您可以声明和在同一行初始化规则排列,就像这样:
INT PowersOfTwo [] = {1,2,4,8,16,32,64,128};
有没有办法复制的自定义类的这种行为?因此,例如:
MyClass的< INT> PowersOfTwo = {1,2,4,8,16,32,64,128};
您可以有一个拷贝构造函数需要一个数组作为它的参数,但你仍然必须声明的previous线阵列。
INT InitializationArray [] = {1,2,4,8,16,32,64,128};
MyClass的< INT> PowersOfTwo = InitializationArray;
您可以实现你的类以这样一种方式,你可以这样写:
MyClass的< INT>阵列;
阵列= 1,2,3,4,5,6,7,8,9,10; //不要担心 - 所有整数去阵!
下面是我的实现:
模板<类T>
MyClass类
{
的std ::矢量< T>项目;
上市: MyClass的&安培;运算符=(const的T&安培;项)
{
items.clear();
items.push_back(项目);
返回*这一点;
}
MyClass的&安培;运营商(常量T&安培;项)
{
items.push_back(项目);
返回*这一点;
}
为size_t尺寸()const的{返回items.size(); }
T&安培;运算符[](为size_t我){回报项目[I] }
常量T&安培;运算符[](为size_t我)const的{返回项目[I] }};诠释主(){ MyClass的< INT>阵列;
阵列= 1,2,3,4,5,6,7,8,9,10;
用于(为size_t我= 0; I< array.Size();我++)
性病::法院LT&;<数组[1] - ;<的std :: ENDL;
返回0;
}
输出:
1
2
3
4
五
6
7
8
9
10
请参阅在线演示: http://www.ideone.com/CBPmj
两个类似的解决方案,您可以在这里看到,我昨天发布的:
<一个href=\"http://stackoverflow.com/questions/5375597/template-array-initialization-with-a-list-of-values\">Template数组初始化与值的列表
编辑:
类似的技巧,你可以做填充现有的STL容器。例如,你可以这样写:
的std ::矢量&lt;&INT GT;伏;
V + = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //的push_back被每INT!
所有你需要重载()
和,
运营商为:
模板&LT; typename的T&GT;
的std ::矢量&lt; T&GT;&安培;运算符+ =(的std ::矢量&lt; T&GT;&安培; V,常量T&安培;项)
{
v.push_back(项目);返回伏;
}
模板&LT; typename的T&GT;
的std ::矢量&lt; T&GT;&安培;运营商(的std ::矢量&lt; T&GT;&安培; V,常量T&安培;项)
{
v.push_back(项目);返回伏;
}
工作演示: http://ideone.com/0cIUD
再次编辑:
我在使用C ++运算符乐趣。现在这个:
的std ::矢量&lt;&INT GT;伏;
V族;&下; 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //将所有的载体!
我觉得这看起来更好!
You can declare and initialize regular arrays on the same line, like so:
int PowersOfTwo[] = {1, 2, 4, 8, 16, 32, 64, 128};
Is there a way to replicate this behavior in custom classes? So, for example:
MyClass<int> PowersOfTwo = {1, 2, 4, 8, 16, 32, 64, 128};
You can have a copy constructor take an array as its parameter, but you still have to declare the array on the previous line.
int InitializationArray[] = {1, 2, 4, 8, 16, 32, 64, 128};
MyClass<int> PowersOfTwo = InitializationArray;
You can implement your class in such a way that you can write this:
MyClass<int> array;
array = 1,2,3,4,5,6,7,8,9,10;//dont worry - all ints goes to the array!!!
Here is my implementation:
template <class T>
class MyClass
{
std::vector<T> items;
public:
MyClass & operator=(const T &item)
{
items.clear();
items.push_back(item);
return *this;
}
MyClass & operator,(const T &item)
{
items.push_back(item);
return *this;
}
size_t Size() const { return items.size(); }
T & operator[](size_t i) { return items[i]; }
const T & operator[](size_t i) const { return items[i]; }
};
int main() {
MyClass<int> array;
array = 1,2,3,4,5,6,7,8,9,10;
for (size_t i = 0 ; i < array.Size() ; i++ )
std::cout << array[i] << std::endl;
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
See online demo : http://www.ideone.com/CBPmj
Two similar solutions you can see here which I posted yesterday :
Template array initialization with a list of values
EDIT:
Similar tricks you can do to populate existing STL containers. For example, you can write this:
std::vector<int> v;
v+=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //push_back is called for each int!
All you need to overload ()
and ,
operator as:
template<typename T>
std::vector<T>& operator+=(std::vector<T> & v, const T & item)
{
v.push_back(item); return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T> & v, const T & item)
{
v.push_back(item); return v;
}
Working demo : http://ideone.com/0cIUD
AGAIN EDIT:
I'm having fun with C++ operator. Now this:
std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //inserts all to the vector!
I think this looks better!
这篇关于实现数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!