Professional Documents
Culture Documents
Chapter 21
Chapter 21
listing 2
// Vector basics.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v; // create zero-length vector
unsigned int i;
return 0;
}
listing 3
// Access a vector using an iterator.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v; // create zero-length vector
int i;
return 0;
}
listing 4
// Demonstrate insert and erase.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v;
unsigned int i;
vector<char>::iterator p = v.begin();
p += 2; // point to 3rd element
return 0;
}
listing 5
// Store a class object in a vector.
#include <iostream>
#include <vector>
using namespace std;
class three_d {
int x, y, z;
public:
three_d() { x = y = z = 0; }
three_d(int a, int b, int c) { x = a; y = b; z = c; }
three_d &operator+(int a) {
x += a;
y += a;
z += a;
return *this;
}
int main()
{
vector<three_d> v;
unsigned int i;
// add objects to a vector
for(i=0; i<10; i++)
v.push_back(three_d(i, i+2, i-3));
return 0;
}
listing 6
// Insert one vector into another.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v, v2;
unsigned int i;
// insert v2 into v
v.insert(p, p2start, p2end);
// display result
cout << "Contents of v after insertion:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
return 0;
}
listing 7
// List basics.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> lst; // create an empty list
int i;
return 0;
}
listing 8
list<char>::iterator p = lst.begin();
while(p != lst.end()) {
cout << *p;
p++;
}
listing 9
// Elements can be put on the front or end of a list.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> lst;
list<char> revlst;
int i;
list<char>::iterator p;
return 0;
}
listing 10
// Sort a list.
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
int main()
{
list<int> lst;
int i;
return 0;
}
listing 11
// Merge two lists.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> lst1, lst2;
int i;
return 0;
}
listing 12
// Store class objects in a list.
#include <iostream>
#include <list>
#include <cstring>
using namespace std;
class myclass {
int a, b;
int sum;
public:
myclass() { a = b = 0; }
myclass(int i, int j) {
a = i;
b = j;
sum = a + b;
}
int getsum() { return sum; }
int main()
{
int i;
return 0;
}
listing 13
// A simple map demonstration.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> m;
int i;
char ch;
cout << "Enter key: ";
cin >> ch;
map<char, int>::iterator p;
return 0;
}
listing 14
m.insert(make_pair((char)('A'+i), i));
listing 15
// Use a map to create a dictionary.
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
class word {
char str[20];
public:
word() { strcpy(str, ""); }
word(char *s) { strcpy(str, s); }
char *get() { return str; }
};
class meaning {
char str[80];
public:
meaning() { strcmp(str, ""); }
meaning(char *s) { strcpy(str, s); }
char *get() { return str; }
};
int main()
{
map<word, meaning> dictionary;
map<word, meaning>::iterator p;
p = dictionary.find(word(str));
if(p != dictionary.end())
cout << "Definition: " << p->second.get();
else
cout << "Word not in dictionary.\n";
return 0;
}
listing 16
// Demonstrate count and count_if.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
return false;
}
int main()
{
char str[] = "STL programming is powerful.";
vector<char> v;
unsigned int i;
int n;
n = count(v.begin(), v.end(), 'p');
cout << n << " characters are p\n";
return 0;
}
listing 17
// Demonstrate remove_copy and replace_copy.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
char str[] = "This is a test.";
vector<char> v, v2(20);
unsigned int i;
return 0;
}
listing 18
// Demonstrate reverse.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
unsigned int i;
reverse(v.begin(), v.end());
return 0;
}
listing 19
// An example of the transform algorithm.
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> xl;
int i;
// put values into list
for(i=0; i<10; i++) xl.push_back(i);
// transform xl
p = transform(xl.begin(), xl.end(), xl.begin(), xform);
return 0;
}
listing 20
char s1[80], s2[80], s3[80];
s1 = "one"; // can't do
s2 = "two"; // can't do
s3 = s1 + s2; // error, not allowed
listing 21
strcpy(s1, "one");
strcpy(s2, "two");
strcpy(s3, s1);
strcat(s3, s2);
listing 22
// A short string demonstration.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("The string class gives ");
string str2("C++ high-powered string handling.");
string str3;
// assign a string
str3 = str1;
cout << str1 << "\n" << str3 << "\n";
// compare strings
if(str3 > str1) cout << "str3 > str1\n";
if(str3 == str1+str2)
cout << "str3 == str1+str2\n";
// input a string
cout << "Enter a string: ";
cin >> str4;
cout << str4;
return 0;
}
listing 23
// Demonstrate insert(), erase(), and replace().
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("This is a test");
string str2("ABCDEFG");
// demonstrate insert()
cout << "Insert str2 into str1:\n";
str1.insert(5, str2);
cout << str1 << "\n\n";
// demonstrate erase()
cout << "Remove 7 characters from str1:\n";
str1.erase(5, 7);
cout << str1 <<"\n\n";
// demonstrate replace
cout << "Replace 2 characters in str1 with str2:\n";
str1.replace(5, 2, str2);
cout << str1 << endl;
return 0;
}
listing 24
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string s1 =
"The string class makes string handling easy.";
string s2;
i = s1.find("class");
if(i!=string::npos) {
cout << "Match found at " << i << endl;
cout << "Remaining string is: ";
s2.assign(s1, i, s1.size());
cout << s2;
}
return 0;
}
listing 25
// Use a map of strings to create a dictionary.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, string> dictionary;
dictionary.insert(pair<string, string>("house",
"A place of dwelling."));
dictionary.insert(pair<string, string>("keyboard",
"An input device."));
dictionary.insert(pair<string, string>("programming",
"The act of writing a program."));
dictionary.insert(pair<string, string>("STL",
"Standard Template Library"));
string s;
cout << "Enter word: ";
cin >> s;
map<string, string>::iterator p;
p = dictionary.find(s);
if(p != dictionary.end())
cout << "Definition: " << p->second;
else
cout << "Word not in dictionary.\n";
return 0;
}