473,287 Members | 1,926 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,287 software developers and data experts.

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 6189
"Kenneth" <wi******@sinaman.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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******@sinaman.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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_back( new MyClass );

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

"Kenneth" <wi******@sinaman.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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******@sinaman.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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_iterator
#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_back(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_iterator< 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_back( t_Node(sz, sz + 0.1, sz * 10) );
}

nlist.display();
nodelist.display();

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::shared_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
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...
4
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
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...
1
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
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...
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...
4
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; ...
2
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...
17
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. ...
12
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 * ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.