Vectors and strings

This lesson contains approximately 25 minutes of video content.

std::vector

An example with std::vector

Consider the following program that "finds" even integer values stored in an std::vector<int> vect and grows another std::vector<int> even_values with copies of the even integers values found.

#include <iostream>
#include <vector>
 
int main() {
  std::vector<int> vect{1, 2, 3, 4, 5}; // creates an std::vector<int> storing 1, 2, 3, 4, 5
  std::vector<int> even_values; // init an empty std::vector<int>
  for (unsigned int i = 0; i < vect.size(); ++i) {
    if (vect.at(i) % 2 == 0)
      even_values.push_back(vect.at(i));
  }
 
  std::cout << "Even values found: " << std::endl;
  for (unsigned int i = 0; i < even_values.size(); ++i) {
    std::cout << even_values.at(i) << std::endl;
  }
}

std::string

An example with std::string

This example focuses on iterating over the characters comprising an input string and making decisions based on those individual characters to formulate a new string through string concatenation. Specifically, we will build a new string revised containing the same characters as the input string minus any period punctuation marks.

std::string input = "Howdy world.";

std::string revised;
for(unsigned int i = 0; i < input.size(); ++i) {
  switch(input.at(i)) {
    case '.' :
      break;
    default :
      revised += input.at(i);
  }
}

std::cout << "input: " << input << std::endl;
std::cout << "revised: " << revised << std::endl;

Activity: Individual words from std::string to std::vector<std::string>

Graded Playground Autograder

Activity Prompt: In this activity, you will implement std::vector<std::string> words_to_vector(std::string str) in utilities.cc. This function must:
  • Takes a single std::string as its argument
  • Return a std::vector where each word (whitespace delimited) in the parameter str is an element, with all occurrences of the punctuation {!, ?, ., ,} removed from it.
Consider the following examples (std::string --> std::vector)
  • Hello, World! --> {"Hello", "World"}
  • What is your name? --> {"What", "is", "your", "name"}
  • Who? What? Where? Why? How? --> {"Who", "What", "Where", "Why", "How"}
Do not include any libraries other than <vector> and <string> in your solution.

Recommended process

Do not overcomplicate this problem by attempting to use language facilities we have not taught. You can solve this problem using a combination of selection, iteration, and string concatenation. That's all you need! Think about how you could iterate through the input string passed to your function and evaluate each character individually. How would you add those characters to another string that will eventually compose a word? You can create an empty string with std::string str_name;. How could you grow an empty string into a word? A string has a push_back behavior that takes a character. How would you ensure that only non-punctuation characters are pushed back? A conditional statement! What does whitespace mean? You're about to observe a new word. How can we clear the contents of str_name? By invoking str_name.clear();!

Don't try to implement your solution to the whole problem at once! First, write a solution that extracts the individual words (with punctuation) from the parameter std::string. In driver.cc, test your implementation by passing an std::string composed of one word (with and without punctuation) as an argument to words_to_vector. Additionally, test words_to_vector by passing an std::string storing a sentence (multiple words and punctuation).

After you're able to extract the words with punctuations from the std::string to the std::vector<std::string>, you should refactor your solution to ignore punctuation. Remember, you can always test your solution by printing the contents of the std::vector<std::string> returned by words_to_vector(std::string str) to standard output. Remember, informal testing is considered a part of each assignment in this course. Please do not neglect it!

#include <iostream> #include <string> #include <vector> #include "utilities.hpp" int main() { std::string phrase = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair."; std::cout << phrase << std::endl; }
#ifndef UTILITIES_HPP #define UTILITIES_HPP #include <string> #include <vector> // function declaration here; provided for you! std::vector<std::string> words_to_vector(std::string str); #endif
#include "utilities.hpp" // define words_to_vector here