S R T B H P N

Chapter-1 Exercises


Programming exercises: C++ Basic Syntax:
  1. Write a function that accepts a std::string by const reference, efficiently reverses the string's character sequence, and returns the reversed string by value.
    Requires copy construction of temp std::string, looping half way through the string, and swapping characters between the first half and the second half. What happens if the string has an odd number of characters?
  2. Repeat the first exercise, but replace the std::string with a std::vector<char>.
    Very similar to the first exercise.
  3. Repeat the first exercise, but replace the std::string with a std::list<char>.
    More complex than the first exercise.
  4. Write code that declares two floating point numbers and initializes one to a know value. Write a function that, using pointers, copies each byte of the intialized number into the other. Verify that after the copy both numbers hold the same value. Write another function that compares the two numbers, byte by byte, using pointers.
    You will want to ensure that no buffer overruns occur, by using the sizeof operator.
  5. Write a function that accepts an integer value and returns a std::list of the last N values entered, where N is a second function parameter.
    That will require you to use a static std::list in the function. Look at cppreference.com for information about std::list.
  6. Write a function that accepts std::vector<double> and displays its elements, where each element is separated by a comma.
    There should be no comma at the end.
  7. Repeat the last exercise, but write each element in a fixed width field, where the field size is a second parameter of the function. If the width is too small to hold the largest of the double elements, increase the size of the field.
    That will require you to step through the collection and convert each element to a std::string representation (look up std::to_string), and find the largest.
  8. Write a lambda that accepts a std::string message and displays it on the console with a second line composed of '-' characters.
    If the lambda prepends the message with a newline, indents it two spaces, and makes the underline string two characters longer, with a one character indent, the result creates a nice title. Can you create the lambda so it also accepts an underline character which defaults to '-'?