学习者关于填充向量的问题< pair< int,string> *> asmember [英] learner's question on populating vector< pair<int, string>* > asmember

查看:76
本文介绍了学习者关于填充向量的问题< pair< int,string> *> asmember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个具有

vector< pair< int,string> * c;

作为成员数据对象。


我需要使用运算符>将值存储到此容器对象中



运营商<<打印容器的内容。


我将这两个操作符都写成非朋友函数。


析构函数删除所有对< b $ b ; int,

向量中的stringpointers。


我怀疑是我正在使用store_pair()函数分配

内存

每对< int,stringpointer并存储到容器中;但是



dtor删除这些指针从而释放内存。


如果此程序中的方法是,请告诉我正确。


以下是完整的程序:


#include< cstdlib>

#include< iostream>

#include< vector>

#include< utility>


using namespace std;


班级考试

{

公开:

考试();

~ Test();


typedef pair< int,stringpair_type;

typedef vector< pair_type * container_type;


void store_pair(const pair_type& arg);

const container_type& container()const {return c; }


私人:

container_type c;

};


测试: :Test():c()

{

}


void Test :: store_pair(const pair_type& arg)

{

pair_type * p = new pair_type(arg.first,arg.second);

c.push_back(p);

}


测试::〜测试()

{

for(container_type :: iterator it = c.begin(); it!= c.end(); +

+ it)

{

delete * it;

*它= 0;

}

}


istream&运算符>>(istream& is,Test& obj)

{

while(is)

{

int x;

string str;

是> x> str;


if(is)

{

Test :: pair_type temp(x,str);

obj.store_pair(temp);

} < br $>
}


返回是;

}


ostream& operator<(ostream& os,const Test& obj)

{

for(Test :: container_type :: const_iterator it =

obj.container()。begin();

it!= obj.container()。end();

++ it)

os<< (** it).first<< " " << (* it) - > second<<结束;


返回操作系统;

}


int main()

{

测试对象;


cin> obj;


cout<< obj;


返回EXIT_SUCCESS;

}


谢谢

V.Subramanian

Consider a class that has
vector< pair<int, string>* c;
as member data object.

I need to use operator>to store values into this container object
and
operator<< to print the contents of the container.

I have written both these operators as non-friend functions.

The destructor deletes all the pair<int, stringpointers in the
vector.

My doubt is that I am using a store_pair() function which allocates
memory
for each pair<int, stringpointer and stores into the container; but
the
dtor deletes these pointers thereby releasing the memory.

Kindly let me know if the approach in this program is correct.

Here is the full program:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>

using namespace std;

class Test
{
public:
Test();
~Test();

typedef pair<int, stringpair_type;
typedef vector<pair_type*container_type;

void store_pair(const pair_type& arg);
const container_type& container() const { return c; }

private:
container_type c;
};

Test::Test() : c()
{
}

void Test::store_pair(const pair_type& arg)
{
pair_type* p = new pair_type(arg.first, arg.second);
c.push_back(p);
}

Test::~Test()
{
for (container_type::iterator it = c.begin(); it != c.end(); +
+it)
{
delete *it;
*it = 0;
}
}

istream& operator>>(istream& is, Test& obj)
{
while (is)
{
int x;
string str;
is >x >str;

if (is)
{
Test::pair_type temp(x, str);
obj.store_pair(temp);
}
}

return is;
}

ostream& operator<<(ostream& os, const Test& obj)
{
for (Test::container_type::const_iterator it =
obj.container().begin();
it != obj.container().end();
++it)
os << (**it).first << " " << (*it)->second << endl;

return os;
}

int main()
{
Test obj;

cin >obj;

cout << obj;

return EXIT_SUCCESS;
}

Thanks
V.Subramanian

推荐答案

4月25日,8:29 * pm,subramanian10 ... @ yahoo.com,India

< subramanian10 ... @ yahoo.comwrote:
On Apr 25, 8:29*pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

考虑一个具有

vector< pair< int,string> * * c;

作为成员数据对象。

我需要使用运算符>将值存储到此容器对象中/>


运算符<<打印容器的内容。


我将这两个操作符都写成非朋友函数。


析构函数删除所有对< b $ b ; int,

向量中的stringpointers。


我怀疑是我正在使用store_pair()函数分配

内存

每对< int,stringpointer并存储到容器中;但是



dtor删除这些指针从而释放内存。


如果此程序中的方法是,请告诉我正确。


以下是完整的程序:


#include< cstdlib>

#include< iostream>

#include< vector>

#include< utility>


using namespace std;


班级考试

{

公开:

考试();

~ Test();


typedef pair< int,stringpair_type;

typedef vector< pair_type * container_type;


void store_pair(const pair_type& arg);

const container_type& container()const {return c; }


私人:

container_type c;


};


Test :: Test():c()

{


}


void Test :: store_pair(const pair_type& arg)

{

* * * * pair_type * p = new pair_type(arg.first,arg.second);

* * * * c.push_back(p);


}


测试::〜测试()

{

* * * * for(container_type :: iterator it = c.begin(); it!= c.end(); +

+ it )

* * * * {

* * * * * * * *删除*它;

* * * * * * * * *它= 0;

* * * *}


}


istream&运算符>>(istream& is,Test& obj)

{

* * * * while(is)

* * * * {

* * * * * * * * int x;

* * * * * * * * string str;

* * * * * * * *是> x> str;


* * * * * * * * if(是)

* * * * * * * * {

* * * * * * * * * * * * Test :: pair_type temp(x,str);

* * * * * * * * * * * * obj.store_pair(临时);

* * * * * * * *}

* * * *}


* * * *返回是;


}


ostream& operator<(ostream& os,const Test& obj)

{

* * * * for(Test :: container_type :: const_iterator it =

obj.container()。begin();

* * * * * * * it!= obj.container()。end();

* * * * * * * ++ it)

* * * * * * * * os<< (** it).first<< " * << (* it) - > second<< endl;


* * * *返回os;


}


int main()

{

* * * *测试对象;


* * * * cin> obj;


* * * * cout<< obj;


* * * *返回EXIT_SUCCESS;


}


谢谢

V.Subramanian
Consider a class that has
vector< pair<int, string>* *c;
as member data object.

I need to use operator>to store values into this container object
and
operator<< to print the contents of the container.

I have written both these operators as non-friend functions.

The destructor deletes all the pair<int, stringpointers in the
vector.

My doubt is that I am using a store_pair() function which allocates
memory
for each pair<int, stringpointer and stores into the container; but
the
dtor deletes these pointers thereby releasing the memory.

Kindly let me know if the approach in this program is correct.

Here is the full program:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>

using namespace std;

class Test
{
public:
Test();
~Test();

typedef pair<int, stringpair_type;
typedef vector<pair_type*container_type;

void store_pair(const pair_type& arg);
const container_type& container() const { return c; }

private:
container_type c;

};

Test::Test() : c()
{

}

void Test::store_pair(const pair_type& arg)
{
* * * * pair_type* p = new pair_type(arg.first, arg.second);
* * * * c.push_back(p);

}

Test::~Test()
{
* * * * for (container_type::iterator it = c.begin(); it != c.end(); +
+it)
* * * * {
* * * * * * * * delete *it;
* * * * * * * * *it = 0;
* * * * }

}

istream& operator>>(istream& is, Test& obj)
{
* * * * while (is)
* * * * {
* * * * * * * * int x;
* * * * * * * * string str;
* * * * * * * * is >x >str;

* * * * * * * * if (is)
* * * * * * * * {
* * * * * * * * * * * * Test::pair_type temp(x, str);
* * * * * * * * * * * * obj.store_pair(temp);
* * * * * * * * }
* * * * }

* * * * return is;

}

ostream& operator<<(ostream& os, const Test& obj)
{
* * * * for (Test::container_type::const_iterator it =
obj.container().begin();
* * * * * * * it != obj.container().end();
* * * * * * * ++it)
* * * * * * * * os << (**it).first << " * " << (*it)->second << endl;

* * * * return os;

}

int main()
{
* * * * Test obj;

* * * * cin >obj;

* * * * cout << obj;

* * * * return EXIT_SUCCESS;

}

Thanks
V.Subramanian



#include< stringin你的程序,它应该可以正常工作。

#include<stringin your program and it should work fine.


os******@gmail.com 写道:

>

#include< stringin你的程序,它应该可以正常工作。
>
#include<stringin your program and it should work fine.



您是否需要引用整条信息才能添加?


-

Ian Collins。

Did you have to quote the entire message just to add that?

--
Ian Collins.


su ** ************@yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

考虑一个有
$的课程b $ b vector< pair< int,string> * c;

作为成员数据对象。


我需要使用运算符>将值存储到此容器对象中



运营商<<打印容器的内容。


我将这两个操作符都写成非朋友函数。


析构函数删除所有对< b $ b ; int,

向量中的stringpointers。


我怀疑是我正在使用store_pair()函数分配

内存

每对< int,stringpointer并存储到容器中;但是



dtor删除这些指针从而释放内存。


如果此程序中的方法是,请告诉我正确。
Consider a class that has
vector< pair<int, string>* c;
as member data object.

I need to use operator>to store values into this container object
and
operator<< to print the contents of the container.

I have written both these operators as non-friend functions.

The destructor deletes all the pair<int, stringpointers in the
vector.

My doubt is that I am using a store_pair() function which allocates
memory
for each pair<int, stringpointer and stores into the container; but
the
dtor deletes these pointers thereby releasing the memory.

Kindly let me know if the approach in this program is correct.



这是一个选项,但考虑在向量中存储小对象(如你的

对< int,string>)而不是指针。然后你就不会了b $ b必须打扰删除它们或在你的插入器中复制一个临时对象。


-

伊恩柯林斯。

It is one option, but consider just storing small objects (like your
pair<int, string>) in the vector rather than pointers. Then you won''t
have to bother deleting them or copying a temporary object in your inserter.

--
Ian Collins.


这篇关于学习者关于填充向量的问题&lt; pair&lt; int,string&gt; *&gt; asmember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆