'for' 内的对象初始化和循环?
Object initialization and looping inside 'for'?
我今天参加了一个面试,面试官问我下面的问题来实现和填写class中的代码。
class I
{
// code to be filled in
.....
.....
.....
};
int main()
{
for(I i=0; i<10; i++){ // to make this for loop work, what needs to be written in the class above?
cout << " " << endl;
}
......
......
return 0;
}
我不是很清楚,无法回答。有人可以让我知道这个问题吗?
您需要实现所需的运算符(<
和 ++
)和匹配的构造函数(I(int)
):
#include <iostream>
using namespace std;
class I
{
private:
int index;
public:
I(const int& i) : index(i) {}
bool operator<(const int& i) {
return index < i;
}
I operator++(int) {
I result(index);
++index;
return result;
}
};
int main()
{
for(I i=0; i<10; i++){ // to make this for loop work, what needs to be written in the class above?
cout << "A" << endl;
}
return 0;
}
查看此声明
for(I i=0; i<10; i++){
由此得出以下构造
I i=0;
i<10;
i++
有效。
所以 class 需要一个(非显式)构造函数,它可以接受一个 int 类型的对象作为参数。 class 应具有可访问的复制或移动构造函数。
对于 class 的对象或当一个操作数可以是 int 类型时,应该声明 operator <
。
而 class 需要后缀递增运算符 operator ++
.
这是一个演示程序。为了清楚起见,我添加了运算符 <<。
#include <iostream>
class I
{
public:
I( int i ) : i( i ) {}
I( const I & ) = default;
bool operator <( const I &rhs ) const
{
return i < rhs.i;
}
I operator ++( int )
{
I tmp( *this );
++i;
return tmp;
}
friend std::ostream & operator <<( std::ostream &, const I & );
private:
int i;
};
std::ostream & operator <<( std::ostream &os, const I &obj )
{
return os << obj.i;
}
int main()
{
for ( I i = 0; i < 10; i++ )
{
std::cout << i << ' ';
}
std::cout << std::endl;
}
程序输出为
0 1 2 3 4 5 6 7 8 9
如果您希望此循环有效
for ( I i = 10; i; )
{
std::cout << --i << ' ';
}
那么 class 定义可以像
#include <iostream>
class I
{
public:
I( int i ) : i( i ) {}
I( const I & ) = default;
explicit operator bool() const
{
return i != 0;
}
I & operator --()
{
--i;
return *this;
}
friend std::ostream & operator <<( std::ostream &, const I & );
private:
int i;
};
std::ostream & operator <<( std::ostream &os, const I &obj )
{
return os << obj.i;
}
int main()
{
for ( I i = 10; i; )
{
std::cout << --i << ' ';
}
std::cout << std::endl;
}
程序输出为
9 8 7 6 5 4 3 2 1 0
我今天参加了一个面试,面试官问我下面的问题来实现和填写class中的代码。
class I
{
// code to be filled in
.....
.....
.....
};
int main()
{
for(I i=0; i<10; i++){ // to make this for loop work, what needs to be written in the class above?
cout << " " << endl;
}
......
......
return 0;
}
我不是很清楚,无法回答。有人可以让我知道这个问题吗?
您需要实现所需的运算符(<
和 ++
)和匹配的构造函数(I(int)
):
#include <iostream>
using namespace std;
class I
{
private:
int index;
public:
I(const int& i) : index(i) {}
bool operator<(const int& i) {
return index < i;
}
I operator++(int) {
I result(index);
++index;
return result;
}
};
int main()
{
for(I i=0; i<10; i++){ // to make this for loop work, what needs to be written in the class above?
cout << "A" << endl;
}
return 0;
}
查看此声明
for(I i=0; i<10; i++){
由此得出以下构造
I i=0;
i<10;
i++
有效。
所以 class 需要一个(非显式)构造函数,它可以接受一个 int 类型的对象作为参数。 class 应具有可访问的复制或移动构造函数。
对于 class 的对象或当一个操作数可以是 int 类型时,应该声明 operator <
。
而 class 需要后缀递增运算符 operator ++
.
这是一个演示程序。为了清楚起见,我添加了运算符 <<。
#include <iostream>
class I
{
public:
I( int i ) : i( i ) {}
I( const I & ) = default;
bool operator <( const I &rhs ) const
{
return i < rhs.i;
}
I operator ++( int )
{
I tmp( *this );
++i;
return tmp;
}
friend std::ostream & operator <<( std::ostream &, const I & );
private:
int i;
};
std::ostream & operator <<( std::ostream &os, const I &obj )
{
return os << obj.i;
}
int main()
{
for ( I i = 0; i < 10; i++ )
{
std::cout << i << ' ';
}
std::cout << std::endl;
}
程序输出为
0 1 2 3 4 5 6 7 8 9
如果您希望此循环有效
for ( I i = 10; i; )
{
std::cout << --i << ' ';
}
那么 class 定义可以像
#include <iostream>
class I
{
public:
I( int i ) : i( i ) {}
I( const I & ) = default;
explicit operator bool() const
{
return i != 0;
}
I & operator --()
{
--i;
return *this;
}
friend std::ostream & operator <<( std::ostream &, const I & );
private:
int i;
};
std::ostream & operator <<( std::ostream &os, const I &obj )
{
return os << obj.i;
}
int main()
{
for ( I i = 10; i; )
{
std::cout << --i << ' ';
}
std::cout << std::endl;
}
程序输出为
9 8 7 6 5 4 3 2 1 0