03 - collections

Collections

  • A collection class (also called an abstract data type or container class) is a data type used to store and organize data in some form.
    These are things like arrays, lists, maps, dictionaries, etc.

We start exploring collections and how to use them appropriately.
Later, we’ll analyze their efficiencies. For now, let’s just focus on how to use them.

Linear

Vector

About

  • A Vector is a collection class representing a list of things.
  • It’s similar to Java's ArrayList, JavaScript’s arrays, and Python’s lists.
  • To make a Vector, use this syntax: Vector <type> name;
  • All elements of a Vector have to have the same type. You specify that type by placing it in after the word Vector.

Stacks

About

  • A Stack is a data structure representing a stack of things.
  • Objects can be pushed on top of the stack or popped from the top of the stack.

Usage

Balancing Parentheses

Queues

About

  • A Queue is a data structure representing a waiting line.
  • Objects can be enqueued to the back of the line or dequeued from the front of the line.
  • No other objects in the queue are visible.

Not Linear

Lexicon

  • Storing a collection of words.

  • A Lexicon is a container that stores a collection of words.

  • The Lexicon is designed to answer the following question efficiently:

  • Given a word, is it contained in the Lexicon?

  • The Lexicon does not support access by index. You can’t, for example, ask what the 137th English word is.

  • However, it does support questions of the form “does this word exist?” or “do any words have this as a prefix?”

Tautonyms
A tautonym is a word formed by repeating the same string twice.
For example: murmur, couscous, papa, etc.

Set

  • Storing a group of whatever you’d like.

Usage

  • You can add a value to a Set by writing set += value;
  • You can remove a value from a Set by writing set -= value;
  • You can check if a value exists in a Set by writing set.contains(value)

About

  • The Set represents an unordered collection of distinct elements.
  • Elements can be added and removed. Duplicates aren’t allowed.
// like values stored in a heap, there is no duplicated element.
Set<int> values = {137, 106, 42};
values += 271;
values += 271; // Has no effect
values -= 106;
values -= 103; // Has no effect
  • You may find it helpful to interpret += as “ensure this item is there” and -= as “ensure this item isn’t there.”

Why Sets

/* Vector
 * Imagine you’re maintaining a shopping list as a Vector.
 * If you add the same item twice, it shows up twice.
 * Each item has a numeric position, which you don’t care about but need to know to remove things.
 *When an item gets removed, other items have their positions shifted, which takes extra time.
 */
Vector<string> toBuy;
toBuy += "Scotch bonnet";
toBuy += "Tomatoes";
toBuy += "Rice";
toBuy += "Curry powder";
toBuy += "Rice"; // Oops...

toBuy.remove(1); // Not "remove tomatoes."

/*
0 Scotch Bonnet        0 Scotch Bonnet         0 Scotch Bonnet
1   Tomatoes           1   Tomatoes            1 Rice
2     Rice        =>   2     Rice         =>   2 Curry Powder
3  Curry Powder        3  Curry Powder         3 Rice
4     Rice             4     Rice
*/



/* Set
 * A shopping list is a great place to use a Set.
 * Sets ignore duplicates, so adding an existing item has no effect.
 * There’s no notion of “the first item on the list,” which matches how you use a shopping list.
 *Removing items is easy; no indices need to be adjusted
 */

Set<string> toBuy;
toBuy += "Scotch bonnet";
toBuy += "Tomatoes";
toBuy += "Rice";
toBuy += "Curry powder";
toBuy += "Rice"; // Okay!

toBuy -= "Tomatoes"; // Clearer!

/* no order
Scotch Bonnet        Scotch Bonnet
  Tomatoes             Rice
    Rice        =>   Curry Powder
  Curry Powder        Rice
     Rice
*/

Map

About

  • It’s analogous to dict in Python, to Map in Java, and to objects (used as key/value stores) in JavaScript.
// The Map class represents a set of key/value pairs.
Map<string, int> heights;

// Each key is associated with a value.
heights["Serbia"] = 153;    // Serbia => 153
heights["Chile"] = 300;     // Chile  => 300

// Given a key, we can look up the associated value.
cout << heights["Chile"] << endl;

// We can loop over the keys in a map with a range- based for loop.
for (string key: heights) {
cout << heights[key] << endl;
}

// We can check whether a key is present in the map.
if (heights.containsKey("Mali") {
    cout << "BCEAO" << endl;
}

Map Autoinsertion

Map<string, int> freqMap;
while (true) {
    string text = getLine("Enter some text: ");
    cout << "Times seen: " << freqMap[text] << endl;
    freqMap[text]++;
}
posted @ 2022-10-22 17:45  铧轩  阅读(19)  评论(0编辑  收藏  举报