Strings and Vectors¶
in C++, we tend to use the standard string
class in place of C-style strings
and we tend to use the standard vector
class in place of C-style arrays
string
and vector
are easier to use and less error-prone than C-style
strings and arrays
Strings¶
the string
type is used for sequences of 0 or more characters
#include "error.h"
#include <string>
using namespace std;
int main() {
cout << "What is your name? ";
string name;
cin >> name;
cout << "Hello, " << name << "!\n";
}
Strings¶
the string
type is used for sequences of 0 or more characters
string pets = "cat\ndog\nbird\n";
cout << pets; // cat
// dog
// bird
cout << pets.size() << endl; // 13
\n
is a single character (newline)
The Empty String¶
the empty string
has no characters and size 0
string empty; // s is empty by default
cout << '"' << empty << '"' << endl // ""
<< empty.size() // 0
<< endl;
Concatenation¶
+
joins strings to make a new string
string hey = "Hey! ";
string krusty = hey + hey + hey + '\n';
cout << krusty;
// Hey! Hey! Hey!
Equality and inequality¶
==
and !=
test string equality/inequality
cout << "Enter new password: ";
string pwd;
cin >> pwd;
cout << "Re-enter password: ";
string check;
cin >> check;
if (pwd == "" || pwd != check) {
cout << "passwords don't match\n";
} else {
cout << "Good!\n";
}
String Comparisons¶
<
, <=
, >
, >=
compare strings by lexicographic (alphabetical)
order
cout << "Enter two strings: ";
string a;
string b;
cin >> a >> b;
if (a < b) {
cout << a << ' ' << b << endl;
} else {
cout << b << ' ' << a << endl;
}
Getting an Entire Line¶
use getline
to get an entire line (cin >>
just gets the first word)
cout << "What is your full name? ";
string name;
getline(cin, name);
cout << name << endl;
Accessing string characters¶
s[i]
is the character at index location i
of string s
string s = "left";
cout << s[0] << endl
<< s[1] << endl
<< s[2] << endl
<< s[3] << endl;
// l
// e
// f
// t
first index position is (always) 0
Processing a string with a ranged for-loop¶
string s = "left";
for(char c : s) { // : read as "in"
cout << c << '\n';
}
// l
// e
// f
// t
“for each char
c
in string s
do the following …”
C Strings and C++ Strings are Different¶
a C-style string is an array of characters ending with \0
a C++ string
is an object that manages all the low-level details
string literals like "apple"
are C-style strings
C Strings and C++ Strings are Different¶
generally, avoid C-style strings in C++ whenver you can
they are harder to use and much more error prone than string
objects
Vectors¶
a vector
is a sequence of 0, or more, objects
vector<int> nums = {5, 2, 1};
cout << nums[0] << endl // 5
<< nums[1] << endl // 2
<< nums[2] << endl; // 1
for(int n : nums) { // : is read "in"
cout << n << endl;
}
// 5
// 2
// 1
Vectors¶
a vector
is a sequence of 0, or more, objects
vector<string> names = {"Pat", "Bob", "Carol"};
cout << names[0] << endl // Pat
<< names[1] << endl // Bob
<< names[2] << endl; // Carol
for(string s : names) { // : is read "in"
cout << s << endl;
}
// Pat
// Bob
// Carol
Vectors¶
a vector
is a sequence of 0, or more, objects
vector<vector<double>> matrix = { { 7.1, 4.2, 5.44},
{-6.8, 2.4, 5.44},
{ 0.7, 0.6, 0.2 }
};
for(vector<double> row : matrix) { // : is read "in"
for(double x : row) {
cout << x << ' ';
}
cout << endl;
}
// 7.1 4.2 5.44
// -6.8 2.4 5.44
// 0.7 0.6 0.2
Adding Items to a Vector¶
v.push_back(x)
appends x
to the right side of vector v
(v
grows if necessary)
vector<string> names; // {}
names.push_back("Pat"); // {"Pat"}
names.push_back("Bob"); // {"Pat", "Bob"}
names.push_back("Carol"); // {"Pat", "Bob", "Carol"}
for(string n : names) {
cout << n << endl;
}
Vector Operations¶
vector
s have many useful built-in operations
vector<int> v;
cout << v.size() << ' ' << v.empty() << endl; // 0 1
v = {4, 3, 6, 7};
vector<int> w = v;
if (v == w) {
cout << "v and w are the same\n";
}
Be Careful¶
vector
s are not range-checked when using []
-notation
vector<int> v = {0, 1, 2};
cout << v[3] // v does not have index location 3
<< endl;
v[3]
compiles and runs (sometimes!) but does not return a meaningful value