









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
Linear Collection Introduction C++ Language Features
Linear Collection Introduction C++ Language Features
Linear Collection Introduction C++ Language Features
Linear Collections: Lists and Arrays
array class
locating an element at a given position takes constant time
faster insertions and deletions
but slower random access
enumerate all elements
similar to NSEnumerator in Objective-C
Linear Collection Introduction C++ Language Features
Linear Collections: Lists and Arrays
std::string s("Hello"); std::vector
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
#include
int main(int argc, char *argv[]) { std::vector
for (int i = 1; i <= 3; i++) vec.push_back(i);
std::vector
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
Linear Collection Introduction C++ Language Features
Templates Namespaces and Operator Overloading
Linear Collection Introduction C++ Language Features
Templates Namespaces and Operator Overloading
→ 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.
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
Linear Collection Introduction C++ Language Features
Templates Namespaces and Operator Overloading
→ allows usage of enumerator++ instead of
enumerator.nextObject()
→ powerful, but dangerous feature
→ needs to be used with care!
→ operator+() redefines the + binary operator
→ operator-() redefines the - binary operator
etc.
→ cout in
→ cin in
→ operator+ to concatenate strings
→ operator[] to index a vector
→ operator* to dereference an iterator
etc.