473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why doesn't this list copy work?

Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I could solve it by looping
manually through the argument's list and call push_back for each
element, but I'd like to know what's wrong with my std::copy() call.
Here's the code with sample output:
#include <algorithm> /* std::copy() */
#include <iostream>
#include <list>

class Unit
{
public:
Unit(int n)
{
m_list.push_back(n);
}

Unit(const Unit& u)
{
/* This call fails to copy the list. */
std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());
}

void add_to_list(int n)
{
m_list.push_back(n);
}

void print_list() const
{
std::list<int>::const_iterator itr = m_list.begin();

while(itr != m_list.end())
{
std::cout << *itr << std::endl;
++itr;
}
}

private:
std::list<int> m_list;
};

int
main()
{
Unit u1(4711);

u1.add_to_list(123);
u1.add_to_list(456);

Unit u2(u1);

std::cout << "Printing u1:" << std::endl;
u1.print_list();

std::cout << "Printing u2:" << std::endl;
u2.print_list();

return 0;
}
Sample output:
$ ./copyctor.exe
Printing u1:
4711
123
456
Printing u2:

Thanks for any replies!

/ E

Aug 11 '05 #1
3 1552
Eric Lilja wrote:
Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I could solve it by looping
manually through the argument's list and call push_back for each
element, but I'd like to know what's wrong with my std::copy() call.
You can't copy something if the destination does not have enough space.
In your case, the Unit being copy-constructed is empty. You *must* use
insert() or push_back() on the list.

You have two solutions: either a manual loop or an iterator adaptor.
Here's the code with sample output:
#include <algorithm> /* std::copy() */
#include <iostream>
#include <list>

class Unit
{
public:
Unit(int n)
{
m_list.push_back(n);
}

Unit(const Unit& u)
{
/* This call fails to copy the list. */
std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());
for (std::list<int>::iterator itor=u.m_list.begin();
itor!=u.m_list.end(); ++itor)
{
u.push_back(*itor);
}

or

std::copy(u.m_list.begin(), u.m_list.end(),
std::back_inserter(m_list));
}

void add_to_list(int n)
{
m_list.push_back(n);
}

void print_list() const
{
std::list<int>::const_iterator itr = m_list.begin();

while(itr != m_list.end())
{
std::cout << *itr << std::endl;
++itr;
}
}

private:
std::list<int> m_list;
};

int
main()
{
Unit u1(4711);

u1.add_to_list(123);
u1.add_to_list(456);

Unit u2(u1);

std::cout << "Printing u1:" << std::endl;
u1.print_list();

std::cout << "Printing u2:" << std::endl;
u2.print_list();

return 0;
}
Sample output:
$ ./copyctor.exe
Printing u1:
4711
123
456
Printing u2:

Jonathan

Aug 11 '05 #2
Eric Lilja wrote:
Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I could solve it by looping
manually through the argument's list and call push_back for each
element, but I'd like to know what's wrong with my std::copy() call.
Here's the code with sample output:
#include <algorithm> /* std::copy() */
#include <iostream>
#include <list>

class Unit
{
public:
Unit(int n)
{
m_list.push_back(n);
}

Unit(const Unit& u)
{
/* This call fails to copy the list. */
std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());
Use the following instead. Well implemented classes like STL will
implement safe copy constructors.

m_list = u.m_list;

}

void add_to_list(int n)
{
m_list.push_back(n);
}

void print_list() const
{
std::list<int>::const_iterator itr = m_list.begin();

while(itr != m_list.end())
{
std::cout << *itr << std::endl;
++itr;
}
}

private:
std::list<int> m_list;
};

int
main()
{
Unit u1(4711);

u1.add_to_list(123);
u1.add_to_list(456);

Unit u2(u1);

std::cout << "Printing u1:" << std::endl;
u1.print_list();

std::cout << "Printing u2:" << std::endl;
u2.print_list();

return 0;
}
Sample output:
$ ./copyctor.exe
Printing u1:
4711
123
456
Printing u2:

Thanks for any replies!

/ E


vector u2 is not updated by std:copy() hence it will still appear empty!
The copy did work and is dangerous in this case as you need to be sure
enough memory is availble to copy to!
To copy the contents of a vector to another just use an assignment as
outlined below.

std::vector<int> a,b;
..
..
// initiase a with some values
..
..
b = a;

John B
Aug 11 '05 #3

"n2xssvv g02gfr12930" <n2*****************@ntlworld.com> wrote in message
news:Xg*****************@newsfe1-gui.ntli.net...
Eric Lilja wrote:
Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I could solve it by looping
manually through the argument's list and call push_back for each
element, but I'd like to know what's wrong with my std::copy() call.
Here's the code with sample output:
#include <algorithm> /* std::copy() */
#include <iostream>
#include <list>

class Unit
{
public:
Unit(int n)
{
m_list.push_back(n);
}

Unit(const Unit& u)
{
/* This call fails to copy the list. */
std::copy(u.m_list.begin(), u.m_list.end(), m_list.begin());


Use the following instead. Well implemented classes like STL will
implement safe copy constructors.

m_list = u.m_list;


Better yet:

Unit(const Unit& u):m_list(u.m_list){}

and your initializer list should contain all of your class' members in the
order they are declared in your clas declaration.

Jeff
Aug 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nick L | last post by:
I've hit a brick wall on something that I'm guessing is pretty simple but it's driving me nuts. I noticed that with python lists, generally when you make a copy of a list (ie, List1 = List2) List1...
1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
2
by: johnny | last post by:
hi all, I wonder why this little script doesn't work, maybe it's the provider not allowing the use of load data infile ( I know some don't let users to run some tasks ), could you please tell me...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
4
by: Sin Jeong-hun | last post by:
List<List<T>a=param; List<List<T>b=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element...
6
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.