Lists

COS2614 - Programming: Contemporary Concepts · Introduction to C++ and Qt 4

Lists

In C++, a list is a data structure that holds a collection of elements. Lists are useful because they allow you to store data in a way that is easy to manage. You can add, remove, and access elements in a list efficiently. Understanding lists is essential for writing effective C++ programs, especially when working with dynamic data.

Key idea: After studying this topic, you should be able to:

  • Define what a list is in C++
  • Explain how to create and manipulate lists
  • Use the copy constructor and assignment operator with lists
  • Understand the concept of conversion constructors

What is a List?

A list is a collection of elements that can be of the same or different types. In C++, you can use the Standard Template Library (STL) to create and manage lists. Lists can grow and shrink in size, allowing for flexible data management.

Creating a List

To create a list in C++, you need to include the list header from the STL. Here is an example of how to create a simple list of integers:

#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> myList;
    return 0;
}

Adding Elements to a List

You can add elements to a list using the push_back() function. This function adds an element to the end of the list. Here is an example:

myList.push_back(10);
myList.push_back(20);
myList.push_back(30);

Accessing Elements in a List

To access elements in a list, you can use an iterator. An iterator is an object that allows you to traverse through the elements of a list. Here is an example of how to use an iterator:

for (list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
    cout << *it << " ";
}

Removing Elements from a List

You can remove elements from a list using the remove() function. This function removes all instances of a specified value. Here is an example:

myList.remove(20);

Copy Constructor and Assignment Operator

In C++, if you do not define a copy constructor or an assignment operator for a class, the compiler will provide default versions. The prototypes for these default functions are as follows:

T::T(const T& other);
T& T::operator=(const T& other);

The copy constructor creates a new object as a copy of an existing object. The assignment operator allows you to assign one object to another.

Example of a Class with a List

Here is an example of a class that uses a list to store integers:

#include <iostream>
#include <list>
using namespace std;

class IntegerList {
public:
    void add(int value) {
        myList.push_back(value);
    }

    void display() {
        for (list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
            cout << *it << " ";
        }
        cout << endl;
    }

private:
    list<int> myList;
};

int main() {
    IntegerList myList;
    myList.add(5);
    myList.add(10);
    myList.display();
    return 0;
}

Conversion Constructors

A conversion constructor allows you to create an object from a single value of a different type. Here is an example:

class Fraction {
public:
    Fraction(int n, int d = 1)
    : m_Numerator(n), m_Denominator(d) {}
private:
    int m_Numerator, m_Denominator;
};

In this example, you can create a Fraction object using a single integer, which defaults to a denominator of 1.

Watch out: Remember that if you use a conversion constructor, the compiler will attempt to convert values automatically. This can lead to unexpected behaviour if you are not careful.

Summary

  • A list is a dynamic collection of elements.
  • Use push_back() to add elements and iterators to access them.
  • The compiler provides default copy constructors and assignment operators.
  • Conversion constructors allow for implicit type conversion.

Check your understanding

  1. What is a list in C++?
  2. How do you add an element to a list?
  3. What is the purpose of a copy constructor?
  4. What does a conversion constructor do?