Small Steps VI — Growing into C++
Use Standard Strings and Containers
Use Standard Strings and Containers
Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.
Meeting standard strings and arrays
What you will learn
A container holds several values. std::string and std::vector are standard tools corresponding to the strings and arrays you know.
Try it
void small_main()
{
std::string text = "Hello";
std::vector<int> numbers = {3, 7, 2, 9};
print(text);
print("Characters: ", text.size());
print("Numbers: ", numbers.size());
print("First number: ", numbers[0]);
}
The length operation is named size() instead of length(). The example prints Hello, Characters: 5, Numbers: 4, and First number: 3.
Bracket positions still start at 0. Do not expect standard containers' [] to report out-of-range errors the way Small does.
Adding elements and reading them one by one
What you will learn
std::vector lets you append elements. A range-based for visits a container's elements one at a time.
Try it
int sum(const std::vector<int>& numbers)
{
int total = 0;
for (int value : numbers)
total = total + value;
return total;
}
void small_main()
{
std::vector<int> numbers = {10, 20, 30};
numbers.push_back(40);
print("Sum: ", sum(numbers));
}
push_back(40) appends 40. for (int value : numbers) receives each element's value in value and adds it to a total. The result is Sum: 100.
Here value is a copied integer, so changing it does not change an array element. size() may have a type different from int; check types when handling large sizes.
Exercise
Create a std::vector<int> containing 5 and 10, add another value with push_back(15), and print them all.
void small_main()
{
std::vector<int> numbers = {5, 10};
// Add 15 and print every value.
}
Hint
Use a range-based for such as for (int value : numbers) to visit every value.
show solution
void small_main()
{
std::vector<int> numbers = {5, 10};
numbers.push_back(15);
for (int value : numbers)
print(value);
}
Try More (Optional)
Exercise
Create text_length taking a const std::string& and returning its length. Print the length of "Small".
int text_length(const std::string& text)
{
// Return the length.
return 0;
}
void small_main()
{
std::string text = "Small";
print(text_length(text));
}
Hint
text.size() returns a standard C++ unsigned size type. This exercise returns int, so explicitly convert with static_cast<int>(text.size()) before returning.
show solution
int text_length(const std::string& text)
{
return static_cast<int>(text.size());
}
void small_main()
{
std::string text = "Small";
print(text_length(text));
}