Learn C++ Online

Vectors and strings

On this page
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&gt 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;
In this activity, you will implement std::vector<std::string> WordsToVector(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

Please 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!

Don't try to implement your solution to the whole problem at once! I recommend that you first create a function that removes the punctuation from an input string and returns a new string, containing all the original words, albeit with the punctuation removed. Before moving on, you can test your implementation driver.cc. Thereafter, you can define another function that returns an std::vector<std::string> that "splits" on whitespace an input string into a vector of "words"; extracting the last word will likely be a special case. You can test your word-extracting function by invoking it from the main function and printing out the contents of the std::vector<std::string> it returns using a for-statement. Finally, you can use these two functions to implement WordsToVector.

#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> WordsToVector(std::string str); #endif
#include "utilities.hpp" // define words_to_vector here
> Click Run to compile and run your code.