关于向量< T>的问题 [英] question on vector<T>
问题描述
考虑以下计划:
#include< iostream>
#include< vector>
使用命名空间std;
class测试
{
public:
Test() ;
朋友ostream& operator<<(ostream& os,const Test& t);
private:
static int counter;
int value;
};
int Test :: counter = 0;
测试::测试()
{
value = ++ counter;
cout<< 来自默认的ctor <<值<<结束;
}
ostream& operator<<(ostream& os,const Test& t)
{
os<< t.value;
返回os;
}
int main()
{
vector< Testtvec(10);
for(vector< Test> :: iterator iter = tvec.begin();
iter!= tvec.end();
++ iter)
cout<< * iter<< endl;
返回0;
}
当我在VC ++ 2005 Express Edition下编译并运行该程序时,
它打印以下
默认ctor 1
1
1 >
1
1
1
1
1
1
1
1
默认ctor只调用一次。我认为默认ctor应该是
调用了10次,因为该语句包含tvec(10),如
vector< Testtvec(10);
我哪里错了。
tvec中的每个Test对象都有相同的值1.为什么会这样?
请解释一下。
谢谢
V.Subramanian
su ************** @ yahoo.com ,印度写道:
>
默认的ctor只被调用一次。我是你的ht默认ctor应该是
调用10次,因为该语句包含tvec(10),如
vector< Testtvec(10);
Where我错了。
tvec中的每个Test对象都有相同的值1.为什么会这样?
这是因为默认构造函数只调用一次。您还需要
来设置复制构造函数。
-
- Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )
标准C ++库扩展:教程和
参考的作者。 ( www.petebecker.com/tr1book )
6月19日凌晨4:16,subramanian10 ... @ yahoo.com,India
< subramanian10 ... @ yahoo.comwrote:
考虑以下程序:
#include< iostream>
#include< vector>
使用命名空间std;
class测试
{
public:
测试();
朋友ostream& operator<<(ostream& os,const Test& t);
private :
static int counter;
int value;
};
int Test :: counter = 0;
Test :: Test()
{
value = ++ counter;
cout<< 来自默认的ctor <<值<<结束;
}
ostream& operator<<(ostream& os,const Test& t)
{
os<< t.value;
返回os;
}
int main()
{
vector< Testtvec(10);
for(vector< Test> :: iterator iter = tvec.begin();
iter!= tvec.end();
++ iter)
cout<< * iter<<结束;
返回0;
}
当我编译并运行此程序时VC ++ 2005 Express Edition,
它打印以下
默认ctor 1
1
1
1
1
1
1
1
1
1
1
默认的ctor只被调用一次。我认为默认的ctor应该是
调用10次,因为该语句包含tvec(10),如
vector< Testtvec(10);
我哪里错了。
tvec中的每个Test对象都有相同的值1.为什么会这样?
请解释一下。
>
谢谢
V.Subramanian
试试这个
测试::测试()
{
value = counter;
cout<< 来自默认的ctor <<值<<结束;
}
测试::测试(const测试& t)
{
value = ++ counter;
cout<< 来自默认复制ctor <<值<<结束;
}
现在当你运行它时,它将从默认的ctor 0输出以下
来自默认副本ctor 1
来自默认副本ctor 2
来自默认副本ctor 3
来自默认副本ctor 4
来自默认复制ctor 5
来自默认复制ctor 6
来自默认复制ctor 7
来自默认复制ctor 8
来自默认复制ctor 9
来自默认复制ctor 10
1
2
3
4
5
6
7
8
9
10
HTH
< blockquote> 6月19日晚上8:45,Pete Becker< p ... @ versatilecoding.comwrote:
subramanian10 ... @ yahoo.com,India写道:
默认ctor只被调用一次。我认为默认ctor应该是
调用10次因为st atement包含tvec(10),如
vector< Testtvec(10);
我哪里错了。
tvec中的每个Test对象都具有相同的值1.为什么会这样?
这是因为默认构造函数只调用一次。您还需要
来设置复制构造函数。
-
- Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )
标准C ++库扩展:教程和
参考的作者。 ( www.petebecker.com/tr1book )
为什么复制构造函数只被调用一次?
问候,
Sarath
Consider the following program:
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;
};
int Test::counter = 0;
Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;
}
ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;
}
int main()
{
vector<Testtvec(10);
for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;
return 0;
}
When I compile and run this program under VC++ 2005 Express Edition,
it prints the following
from default ctor 1
1
1
1
1
1
1
1
1
1
1
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
Kindly explain.
Thanks
V.Subramanian
su**************@yahoo.com, India wrote:>
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
It''s because the default constructor is only called once. You also need
to instrument the copy constructor.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
On Jun 19, 4:16 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:Consider the following program:
#include <iostream>
#include <vector>
using namespace std;
class Test
{
public:
Test();
friend ostream &operator <<(ostream &os, const Test &t);
private:
static int counter;
int value;
};
int Test::counter = 0;
Test::Test()
{
value = ++counter;
cout << "from default ctor " << value << endl;
}
ostream &operator <<(ostream &os, const Test &t)
{
os << t.value;
return os;
}
int main()
{
vector<Testtvec(10);
for (vector<Test>::iterator iter = tvec.begin();
iter != tvec.end();
++iter)
cout << *iter << endl;
return 0;
}
When I compile and run this program under VC++ 2005 Express Edition,
it prints the following
from default ctor 1
1
1
1
1
1
1
1
1
1
1
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
Kindly explain.
Thanks
V.SubramanianTry this
Test::Test()
{
value = counter;
cout << "from default ctor " << value << endl;
}
Test::Test(const Test& t)
{
value = ++counter;
cout << "from default copy ctor " << value << endl;
}
Now when you run it, it would output the followng
from default ctor 0
from default copy ctor 1
from default copy ctor 2
from default copy ctor 3
from default copy ctor 4
from default copy ctor 5
from default copy ctor 6
from default copy ctor 7
from default copy ctor 8
from default copy ctor 9
from default copy ctor 10
1
2
3
4
5
6
7
8
9
10
HTH
On Jun 19, 8:45 pm, Pete Becker <p...@versatilecoding.comwrote:subramanian10...@yahoo.com, India wrote:
The default ctor is called only once.I thought default ctor should be
called 10 times because the statement contains tvec(10) as in
vector<Testtvec(10);
Where am I going wrong.
Also every Test object in tvec has the same value 1. Why is it so ?
It''s because the default constructor is only called once. You also need
to instrument the copy constructor.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)Why the copy constructor being called only once?
Regards,
Sarath
这篇关于关于向量< T>的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!