Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Linear Collections in C++: Understanding Vectors, Lists and Language Features, Study notes of C programming

An introduction to linear collections in C++ with a focus on vectors and lists. It covers the basics of C++ language features such as templates, namespaces, and operator overloading. The authors also discuss various methods for manipulating linear collections, including locating elements, merging lists, and enumerating arrays. examples and explanations of useful methods such as front(), back(), empty(), reverse(), splice(), and more.

What you will learn

  • What is the difference between std::vector and std::list?
  • What are some useful methods for manipulating C++ vectors and lists?
  • What are linear collections in C++?
  • How does operator overloading work in C++?
  • How do you locate an element in a C++ vector or list?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

obesix
obesix 🇺🇸

4.2

(18)

239 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linear Collection Introduction
C++ Language Features
C++ Vectors, Lists and Language Features
2501ICT Nathan
René Hexel and Joel Fenwick
School of Information and Communication Technology
Griffith University
Semester 1, 2011
René Hexel and Joel Fenwick C++ Vectors, Lists and Language Features
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Linear Collections in C++: Understanding Vectors, Lists and Language Features and more Study notes C programming in PDF only on Docsity!

Linear Collection Introduction C++ Language Features

C++ Vectors, Lists and Language Features

2501ICT Nathan

René Hexel and Joel Fenwick

School of Information and Communication Technology

Griffith University

Semester 1, 2011

Linear Collection Introduction C++ Language Features

Outline

1 Linear Collection Introduction

Linear Collections: Lists and Arrays

2 C++ Language Features

Templates

Namespaces and Operator Overloading

Linear Collection Introduction C++ Language Features

Linear Collections: Lists and Arrays

C++ Arrays

std::vector

array class

locating an element at a given position takes constant time

std::list

faster insertions and deletions

but slower random access

Iterators

enumerate all elements

similar to NSEnumerator in Objective-C

Linear Collection Introduction C++ Language Features

Linear Collections: Lists and Arrays

C++ Vector and List Example

Example (prints: l3 has 1 element starting with

Hello)

std::string s("Hello"); std::vector v1; // an empty vector v1.assign(3, 0); // 3 zero elements std::vector<std::string> v2(1, s); // a vector with one string std::list<std::string> l1(1, s); // a list with one string std::list<std::string> l2(l1); // copy l1 into l l2.merge(l1); // merge l1 into l

if (l1 == l2) // same content? printf("l1 is equal to l2 -- how come?\n");

std::list<std::string> l3(l2); // copy l2 into l l3.unique(); // remove duplicates int count3 = l3.size(); // number of elements

const char *first = l3.front().c_str(); // first element as char * printf("l3 has %d element starting with %s\n", count3, first);

Linear Collection Introduction C++ Language Features

Linear Collections: Lists and Arrays

Enumerating Array Example

Example (prints: 1 2 3 )

#include #include

int main(int argc, char *argv[]) { std::vector vec;

for (int i = 1; i <= 3; i++) vec.push_back(i);

std::vector::iterator enumerator = vec.begin(); // iterator

while (enumerator != vec.end()) // loop through array printf("%d ", (^) *enumerator++); // print each element

printf("\n");

return EXIT_SUCCESS; }

Linear Collection Introduction C++ Language Features

Templates Namespaces and Operator Overloading

Templates

C++ Templates

Linear Collection Introduction C++ Language Features

Templates Namespaces and Operator Overloading

Namespaces

Using C++ Namespaces

Linear Collection Introduction C++ Language Features

Templates Namespaces and Operator Overloading

Namespaces

The Problem: two types, variables, or functions have the

same name

→ Objective-C uses a prefix such as NS (e.g. NSString for

the string class)

→ C++ uses namespaces

The std Namespace

used for the standard C++ classes

→ std::string, std::vector, std::list, etc.

using namespace std;

should come right after the #include part

avoids having to write std:: all the time

→ makes code more readable

→ use only in .cc (not .h) files!

always write full names in header files!

Linear Collection Introduction C++ Language Features

Templates Namespaces and Operator Overloading

Operator Overloading

Operator Overloading in

C++

Linear Collection Introduction C++ Language Features

Templates Namespaces and Operator Overloading

Operator Overloading

C++ allows class methods to override standard operators

→ allows usage of enumerator++ instead of

enumerator.nextObject()

→ powerful, but dangerous feature

→ needs to be used with care!

Method name is operator followed by the actual operator

→ operator+() redefines the + binary operator

→ operator-() redefines the - binary operator

etc.

Used a lot in the C++ std classes

→ cout in for standard output

→ cin in for standard input

→ operator+ to concatenate strings

→ operator[] to index a vector

→ operator* to dereference an iterator

etc.