Explain the following initializations. Indicate if any are in error, and if so, why.
int ia[7] = { 0, 1, 1, 2, 3, 5, 8 };
string sa[6] = {
"Fort Sumter", "Manassas", "Perryville",
"Vicksburg", "Meridian", "Chancellorsville" };
(a) vector<string> svec(sa, sa+6);
(b) list<int> ilist( ia+4, ia+6);
(c) vector<int> ivec(ia, ia+8);
(d) list<string> slist(sa+6, sa);
Show an example of each of the four ways to create and initialize a vector. Explain what values each vector contains.
Explain the differences between the constructor that takes a container to copy and the constructor that takes two iterators.
Define a list that holds elements that are deques that hold ints.
Why can we not have containers that hold iostream objects?
Given a class type named Foo that does not define a default constructor but does define a constructor that takes int values, define a list of Foo that holds 10 elements.
What is wrong with the following program? How might you correct it?
list<int> lst1;
list<int>::iterator iter1 = lst1.begin(),
iter2 = lst1.end();
while (iter1 < iter2) /* . . . */
Assuming vec_iter is bound to an element in a vector that holds strings, what does this statement do?
if (vec_iter->empty()) /* . . . */
Write a loop to write the elements of a list in reverse order.
Which, if any, of the following iterator uses are in error?
const vector< int > ivec(10);
vector< string > svec(10);
list< int > ilist(10);
(a) vector<int>::iterator it = ivec.begin();
(b) list<int>::iterator it = ilist.begin()+2;
(c) vector<string>::iterator it = &svec[0];
(d) for (vector<string>::iterator
it = svec.begin(); it != 0; ++it)
// ...
What are the constraints on the iterators that form iterator ranges?
Write a function that takes a pair of iterators and an int value. Look for that value in the range and return a bool indicating whether it was found.
Rewrite the program that finds a value to return an iterator that refers to the element. Be sure your function works correctly if the element does not exist.
Using iterators, write a program to read a sequence of strings from the standard input into a vector. Print the elements in the vector.
Rewrite the program from the previous exercise to use a list. List the changes you needed to change the container type.
What type should be used as the index into a vector of ints?
What type should be used to read the elments in a list of strings?
Write a program to copy elements from a list of ints into two deques. The list elements that are even should go into one deque and those that are odd into the second.
Assuming iv is a vector of ints, what is wrong with the following program? How might you correct the problem(s)?
vector<int>::iterator mid = iv.begin() + iv.size()/2;
while (vector<int>::iterator iter != mid)
if (iter == some_val)
iv.insert(iter, 2 * some_val);
Write a program to compare whether a vector<int> contains the same elements as a list<int>.
Assuming c1 and c2 are containers, what constraints does the following usage place on the element types in c1 and c2?
if (c1 < c2)
What, if any, constraints are there on c1 and c2?
Given that vec holds 25 elements, what does vec.resize(100) do? What if we next wrote vec.resize(10)?
What, if any, restrictions does using resize with a single size argument place on the element types?
Write a program that fetches the first element in a vector. Do so using at, the subscript operator, front, and begin. Test the program on an empty vector.
What happens in the program that erased a range of elements if val1 is equal to val2. What happens if either val1 or val2 or both are not present.
Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list and the even values from your vector.
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
Write a program to process a list of strings. Look for a particular value and, if found, remove it. Repeat the program using a deque.
Write a program to assign the elements from a list of char* pointers to C-style character strings to a vector of strings.
Explain the difference between a vector's capacity and its size. Why is it necessary to support the notion of capacity in a container that stores elements contiguously but not, for example, in a list?
Write a program to explore the allocation stragegy followed by the library you use for vector objects.
Can a container have a capacity less than its size? Is a capacity equal to its size desirable? Initially? After an element is inserted? Why or why not?
Explain what the following program does:
vector<string> svec;
svec.reserve(1024);
string text_word;
while (cin >> text_word)
svec.push_back(text_word);
svec.resize(svec.size()+svec.size()/2);If the program reads 256 words, what is its likely capacity after it is resized? What if it reads 512? 1,000? 1,048?
Which is the most appropriatea vector, a deque, or a listfor the following program tasks? Explain the rationale for your choice. If there is no reason to prefer one or another container explain why not?
Read an unknown number of words from a file for the purpose of generating English language sentences.
Read a fixed number of words, inserting them in the container alphabetically as they are entered. We'll see in the next chapter that associative containers are better suited to this problem.
Read an unknown number of words. Always insert new words at the back. Remove the next value from the front.
Read an unknown number of integers from a file. Sort the numbers and then print them to standard output.
Use iterators to change the characters in a string to uppercase.
Use iterators to find and to erase each capital letter from a string.
Write a program that initializes a string from a vector<char>.
Given that you want to read a character at a time into a string, and you know that the data you need to read is at least 100 characters long, how might you improve the performance of your program?
Write a program that, given the string
"ab2c3d7R4E6"
finds each numeric character and then each alphabetic character. Write two versions of the program. The first should use find_first_of, and the second find_first_not_of.
Write a program that, given the strings
string line1 = "We were her pride of 10 she named us:";
string line2 = "Benjamin, Phoenix, the Prodigal"
string line3 = "and perspicacious pacific Suzanne";
string sentence = line1 + ' ' + line2 + ' ' + line3;
counts the number of words in sentence and identifies the largest and smallest words. If several words have the largest or smallest length, report all of them.
Write a program that accepts the following two strings:
string q1("When lilacs last in the dooryard bloom'd");
string q2("The child is father of the man");
Using the assign and append operations, create the string
string sentence("The child is in the dooryard");
Write a program that, given the strings
string generic1("Dear Ms Daisy:");
string generic2("MrsMsMissPeople");
implements the function
string greet(string form, string lastname, string title,
string::size_type pos, int length);
using the replace operations, where lastname replaces Daisy and pos indexes into generic2 of length characters replacing Ms. For example, the following
string lastName("AnnaP");
string salute = greet(generic1, lastName, generic2, 5, 4);
returns the string
Dear Miss AnnaP:
Write a program to read a series of words into a stack.
Use a stack to process parenthesized expressions. When you see an open parenthesis, note that it was seen. When you see a close parenthesis after an open parenthesis, pop elements down to and including the open parenthesis off the stack. push a value onto the stack to indicate that a parenthesized expression was replaced
No comments:
Post a Comment