substr
(“substring”) member function of class
string.
string:
get input from an object of class
istringstream,
just like we get input from the object
cin.
find_first_not_of
to find the exact point at which you want to dismember a
string.
transform
all the values in a container such as a
vector
or a
string.
transform
algorithm be a
lambda function
(an anonymous function).
c++ -o ~/bin/pig pig.C ls -l ~/bin/pig pig < newcolossus.txt
ESTIONQUAY.
pig.C,
change
//Index of first non-consonant character.
const string::size_type c
{word.find_first_not_of(consonants)};
to
//Index of first non-consonant character.
string::size_type c {0};
if (word.substr(0, 2) == "QU") {
c = 2;
} else {
c = word.find_first_not_of(consonants);
}
This example is from The Practice of Programming (1999) by Brian W. Kernighan and Rob Pike.
shuffle
algorithm
supersedes the
random_shuffle
algorithm.
vector
of
strings.
The input file is Ernest Hemingway’s novel
The Sun Also Rises
(1926).
The “stream editor”
sed
changes apostrophe to single quote,
“quotation marks” to double quotes.
nroff
packs the words of input
into lines of output that each contain several words.
When using
more,
press space bar or
q.
c++ -o ~/bin/shuffle2 shuffle2.C ls -l ~/bin/shuffle2 wget -O - https://gutenberg.org/cache/epub/67138/pg67138.txt | awk 'NR >= 95' | sed $'s/\u2019/\u0027/g;s/\u201C/\u022/g;s/\u201D/\u022/g' > rises.txt shuffle2 < rises.txt | more shuffle2 < rises.txt | nroff | more shuffle2 < rises.txt | nroff > shuffle2.txt
stack
is LIFO (last in, first out): last hired, first fired.
queue
is FIFO (first in, first out): people standing on line,
waiting to be served.
operator[]
member function of class
map
can add a new
pair
to the
map.
map
holds a
vector
of all the suffixes that came after occurrences of the prefix in the input.
c++ -o ~/bin/markov prefix.C main.C markov < rises.txt | more (Press space bar or q) markov < rises.txt | nroff | more (Press space bar or q) markov < rises.txt | nroff > main.txt rm rises.txt
animal.C,
animaldialog.txt,
animal.txt
“Yes” goes to the lower left;
“No” goes to the lower right.
languages.cgi
languages.C
by
jc208
bengli.cgi
bengali.C
by
kwesner1.
Must use wide
wcout,
not plain old
cout.
CtoF.html
CtoF.C
by
mrd22.
Try −40° Celsius.
weightagain.html
weightagain.C.
by
ljl8.
vector<body>
to a
vector<pair<string, double>>.
#include <string>
#include <utility>
#include <vector>
const vector<pair<string, double>> aduetog {
{"", 0.0},
{"Mercury", 0.38},
{"Venus", 0.91},
{"Earth", 1.00},
{"Mars", 0.38},
{"Jupiter", 2.34},
{"Saturn", 1.06},
{"Uranus", 0.92},
{"Neptune", 1.19}
};
if (planet < 1 || planet >= aduetog.size()) { //Don't count them yourself.
error message;
}
const auto& p {vector[planet]}; //p is a refernce to a pair<string, double>
const double weightcalc {weight * p.second};
cout << "<BR/"> << p.first << " has " << p.second
<< " times as much gravity as the Earth.\n";
cout << "<BR/>Your weight on " << p.first << " is " << weightcalc << " pounds.\n";
magnolia.html
magnolia.C
by
jmm56.
string base; //The constructor with no arguments puts "" into the string.instead of
string base {""}; //Call the constructor with one argument.
latin.cgi
Latin.C
by
an151.
See the
Unicode
code chart.