Consider:
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
# include <list>
using std::list;
struct my_struct
{
std::string mName;
int mValue;
};
typedef std::list<my_struct> my_list;
int main()
{
my_list cc;
my_struct s;
s.mName = "Foo";
s.mValue = 42;
cc.push_back(s); // add contents
s.mName = "Bar";
s.mValue = 67;
cc.push_back(s); // add contents
my_list::iterator it(cc.begin());
for (; it != cc.end(); ++it)
{
cout << "Name = " << (*it).mName.c_str()
<< " Value = " << (*it).mValue << endl;
}
it = cc.begin();
for (; it != cc.end(); ++it)
{
if (it->mName == "Foo")
{
//cout << "found " << '\n';
//cc.erase(it); // exception. How do I erase "Foo"??
}
}
//my_struct* ptr = *it;
my_struct* ptr = new my_struct;
if (!ptr)
std::cerr << "memory allocation failure " << '\n';
it = cc.begin();
for (; it != cc.end(); ++it)
//ptr = it // assign contents of iterator to memory?
delete ptr;
}
The standard list container my_list holds a composite type my_struct.
At issue is the need to 'erase' Foo and it's corresponding value of
42. How do I achieve this?
ptr is a block of memory that's been allocated the sizeof my_struct.
Is it possible to 'assign' the contents of the iterator to that
memory? Silly though this may seem, a classmate/lab partner was
opting to do as such and I informed him. 1. Doesn't make sense and
2. I don't think that can be done.