473,573 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can <list> be applied to user-defined class in C++?

<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?

Thanks!

Oct 21 '05 #1
5 6225
"Kenneth" <wi******@sinam an.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?

Thanks!


yes!

but there are certain requirements... i think you need public default and
copy constructor (maybe assignment operator too, not sure, check the C++
standard, or any STL bood)

Oct 21 '05 #2
"Kenneth" <wi******@sinam an.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?

Thanks!


Yeah. I do it all the time. Pretty much the same way you do it with built
in primitive types.

If your class does any memory management (new, delete) itself make sure you
have copy and assignment methods.

vector<MyClass> MyVector;

MyVector.push_b ack( new MyClass );

MyClass TempInstance;
MyVector.push_b ack( TempInstance );
Oct 21 '05 #3

"Kenneth" <wi******@sinam an.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?


Certainly.

class C
{
int i;
public:
C(int arg) : i(arg)
{
}
};

std::list<C> my_list;

The class can be as simple or as complex as
you like (but of course still subject to
'resource allocation' issues, e.g. the
'rule of three').

-Mike
Oct 21 '05 #4

"Kenneth" <wi******@sinam an.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| <list> seems to be a powerful structure to store the related nodes in
| memory for fast operations, but the examples I found are all related
to
| primitive type storage.
| I'm doing a project on C++ with my defined classes to be added to
| linked list structure so as to facilitate the operation of all
| instances of defined classes. Is that possible to apply such classes
to
| <list> or <Vector> structure?
|

Not only can a list store some particular node-type, but virtually any
node type, including your own. I've left out one of the requirements,
the assignment operator for the nodes. The std::list container itself
can be encapsulated within a templated class with your own interface.

#include <iterator> // for std::ostream_it erator
#include <algorithm> // for std::copy
#include <list>
#include <ostream>
#include <iostream>

// my own list container class
template< class T >
class TList
{
std::list< T > m_list;
public:
TList() : m_list() { }
~TList() { }
void push_back(const T& t)
{
m_list.push_bac k(t);
}
void pop_back()
{
m_list.pop_back ();
}
void display() const
{
std::cout << "list contents:\n ";
std::copy( m_list.begin(),
m_list.end(),
std::ostream_it erator< T >( std::cout, " ") );
std::cout << std::endl;
}
};

// a templated, complex Node type
template< class N, class D, class L>
class Node
{
N m_n;
D m_d;
L m_l;
public:
Node(N n, D d, L l) : m_n(n), m_d(d), m_l(l) { }
Node(const Node& copy)
{
m_n = copy.m_n;
m_d = copy.m_d;
m_l = copy.m_l;
}
~Node() { }
/* friends */
friend std::ostream&
operator<<(std: :ostream& os, const Node& a)
{
os << a.m_n << ", " << a.m_d << ", " << a.m_l;
return os << std::endl;
}
};

int main()
{
TList<int> nlist; // a list of integers
typedef Node<int, double, long> t_Node;
TList< t_Node > nodelist; // a list of complex nodes

for ( int sz = 0; sz < 10; ++sz)
{
nlist.push_back (sz);
nodelist.push_b ack( t_Node(sz, sz + 0.1, sz * 10) );
}

nlist.display() ;
nodelist.displa y();

return 0;
}

/* output:

list contents:
0 1 2 3 4 5 6 7 8 9
list contents:
0, 0.1, 0
1, 1.1, 10
2, 2.1, 20
3, 3.1, 30
4, 4.1, 40
5, 5.1, 50
6, 6.1, 60
7, 7.1, 70
8, 8.1, 80
9, 9.1, 90

*/

The same applies to any other standard container (std::vector,
std::queue, std::deque, etc) except that associative containers (set,
map, multiset, multimap, etc) have additional requirements other than
copy ctor and operator=.

Even the node type itself can be composed of other templated user-types.
And that would not require any modification of either the Node or TList
class (You can derive from TList to provide supplimentary features).
Comparitively, the work required is minute when you consider the
flexibility and reusability of the templated classes.

Oct 21 '05 #5

Kenneth wrote:
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?

Thanks!


For non-abstract class, yes.
However, if you're trying to store derived types using a base type,
then you would have to use a pointer, or a smart pointer.

vector<foo*> vMyFoo;
//Or even better, use smart pointer
vector<boost::s hared_ptr<foo> > vMyFoo; //Boost shared pointer

vector<copy_ptr <foo> > vMyFoo; //A clone pointer

I recommend the clone pointer over boost shared pointer because it
works like concrete types and when making comparisons it lets you use
value semantics.

You can download the copy_ptr from the following link:
http://code.axter.com/copy_ptr.h

You can also use a COW (Copy On Write) smart pointer:
http://code.axter.com/cow_ptr.h

Oct 21 '05 #6

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

Similar topics

5
8699
by: G?nter Omer | last post by:
Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL <list> but it refuses to work. The following errors occure: "test.h" line 29 type name expected "test.h" line 29 declaration missing
4
1288
by: lallous | last post by:
Hello Given this: list<mystruct_t> lst; lst.push_back(item1); lst.push_back(item2); lst.push_back(item3);
2
2063
by: Tom Vogel | last post by:
I'd like to use the XML Documentation Tags to comment my C# code. But many of the tags do not have any effect when I execute the "Build Comment Web Pages" menu. For example, the <list> tag gets reendered as is. The resulting HTML page contains the exact same tags, which are not valid HTML. The same with <see> or <see also>. Isn't this...
1
1428
by: Steffo | last post by:
Why can't I use stl list in my dll. I'ts no problem with vector, but when trying list I get the followning error msg: error C2061: syntax error : identifier 'list' Hu!! S.
3
2228
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using namespace std; ...... vector <Vector> vectorlist;
4
33118
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 manually. Is there any more efficent way to do that? Something like, List<List<T>b=CreateClone(a); Thanks.
4
2513
by: glumaka | last post by:
Hi there 1 This part has no problem with compilation, but further I don't know how to access "item". Pls help ! Thx ! { typedef double tab; std::list<tab> tbBigList; tab tb; tb=0.1; tb=0.2;
2
2231
by: per9000 | last post by:
Hi, *background* I want a class containing an int (a list of sets of integer). This should be hidden for the user and he/she should be able to insert his/her favourite data structure so to be a little fancy I thought I'd do a Ctor like this: // this is the "core": private int sets;
17
2087
by: Cralis | last post by:
I am trying to populate a ListView with a list of 'Models' of cars. I have a data object class for my models, which has a function, 'getListOfModels', which I want to retuyrn a <Listof models. In my data object, I have also created a class to hold each model, as shown below. No database work has been done. Just adding a model, and trying...
12
2633
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * Write a program to copy elements from a list of "ints" * to 2 "deques". The list elements that are even should go into one deque * and even...
0
7674
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7989
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7744
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6369
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5271
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2170
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.