S
subramanian100in
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, string> pointers in the
vector.
My doubt is that I am using a store_pair() function which allocates
memory
for each pair<int, string> pointer 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, string> pair_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:air_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
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, string> pointers in the
vector.
My doubt is that I am using a store_pair() function which allocates
memory
for each pair<int, string> pointer 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, string> pair_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:air_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