C ++运算符重载<<与向量
C++ operator overloading << with vector
大家好,我是 C++ 的新手,我对这个运算符有疑问:(在 Whosebug 中也是新的)
这是我的 class 测试列表:
class TestList{
public:
TestList() : listItems(10), position(0){};
TestList(int k) : listItems(k), position(0){};
int listItems;
int position;
std::vector<int> arr;
};
//my current operator is: What should be changed?
ostream& operator <<(ostream&, const TestList& tlist, int input){
os << tlist.arr.push_back(input);
return os;
}
//
int main() {
testList testlist(5);
testlist << 1 << 2 << 3; //how should I overload the operator to add these number to testlist.arr ?
return 0;
}
我希望有人能帮助我或给我任何提示? :)
我认为你的意思如下
TestList & operator <<( TestList &tlist , int input )
{
tlist.arr.push_back( input );
return tlist;
}
这是一个演示程序
#include <iostream>
#include <vector>
class TestList{
public:
TestList() : listItems(10), position(0){};
TestList(int k) : listItems(k), position(0){};
int listItems;
int position;
std::vector<int> arr;
};
TestList & operator <<( TestList &tlist , int input )
{
tlist.arr.push_back( input );
return tlist;
}
std::ostream & operator <<( std::ostream &os, const TestList &tlist )
{
for ( const auto &item : tlist.arr )
{
std::cout << item << ' ';
}
return os;
}
int main()
{
TestList testlist(5);
testlist << 1 << 2 << 3;
std::cout << testlist << '\n';
return 0;
}
程序输出为
1 2 3
你甚至可以写出这两个语句
testlist << 1 << 2 << 3;
std::cout << testlist << '\n';
只有一个声明
std::cout << ( testlist << 1 << 2 << 3 ) << '\n';
注意你的声明有错别字
testList testlist(5);
应该有
TestList testlist(5);
其他的回答完全正确,我只是想在operator<<
上说一些笼统的事情。它始终具有签名 T operator<<(U, V)
,因为它始终是二元运算符,所以它必须恰好有两个参数。自链
a << b << c;
被评估为
(a << b) << c;
// That calls
operator<<(operator<<(a, b), c);
类型 T
和 U
通常应该是相同的,或者至少是兼容的。
此外,将 operator<<
的结果分配给某些东西(如 result = (a << b)
)是可能的,但很奇怪。一个好的经验法则是 "My code should not be weird"。因此,类型 T
应该主要是一个引用(所以 X&
),否则它只会是一个未使用的临时副本。这在大多数时候是毫无用处的。
所以在 90% 的情况下,你的 operator<<
应该有签名
T& operator<<(T&, V);
大家好,我是 C++ 的新手,我对这个运算符有疑问:(在 Whosebug 中也是新的)
这是我的 class 测试列表:
class TestList{
public:
TestList() : listItems(10), position(0){};
TestList(int k) : listItems(k), position(0){};
int listItems;
int position;
std::vector<int> arr;
};
//my current operator is: What should be changed?
ostream& operator <<(ostream&, const TestList& tlist, int input){
os << tlist.arr.push_back(input);
return os;
}
//
int main() {
testList testlist(5);
testlist << 1 << 2 << 3; //how should I overload the operator to add these number to testlist.arr ?
return 0;
}
我希望有人能帮助我或给我任何提示? :)
我认为你的意思如下
TestList & operator <<( TestList &tlist , int input )
{
tlist.arr.push_back( input );
return tlist;
}
这是一个演示程序
#include <iostream>
#include <vector>
class TestList{
public:
TestList() : listItems(10), position(0){};
TestList(int k) : listItems(k), position(0){};
int listItems;
int position;
std::vector<int> arr;
};
TestList & operator <<( TestList &tlist , int input )
{
tlist.arr.push_back( input );
return tlist;
}
std::ostream & operator <<( std::ostream &os, const TestList &tlist )
{
for ( const auto &item : tlist.arr )
{
std::cout << item << ' ';
}
return os;
}
int main()
{
TestList testlist(5);
testlist << 1 << 2 << 3;
std::cout << testlist << '\n';
return 0;
}
程序输出为
1 2 3
你甚至可以写出这两个语句
testlist << 1 << 2 << 3;
std::cout << testlist << '\n';
只有一个声明
std::cout << ( testlist << 1 << 2 << 3 ) << '\n';
注意你的声明有错别字
testList testlist(5);
应该有
TestList testlist(5);
其他的回答完全正确,我只是想在operator<<
上说一些笼统的事情。它始终具有签名 T operator<<(U, V)
,因为它始终是二元运算符,所以它必须恰好有两个参数。自链
a << b << c;
被评估为
(a << b) << c;
// That calls
operator<<(operator<<(a, b), c);
类型 T
和 U
通常应该是相同的,或者至少是兼容的。
此外,将 operator<<
的结果分配给某些东西(如 result = (a << b)
)是可能的,但很奇怪。一个好的经验法则是 "My code should not be weird"。因此,类型 T
应该主要是一个引用(所以 X&
),否则它只会是一个未使用的临时副本。这在大多数时候是毫无用处的。
所以在 90% 的情况下,你的 operator<<
应该有签名
T& operator<<(T&, V);