C++ 基础练习 - Chapter 11 (英文版)

Review Questions:

11.1 What are input and output streams?

Answer:

The stream that supplies data to the program is known as input stream and one that receives data from the program is known as output stream.

11.2 What are the steps involved in using a file in a C++ program?

Answer:

The I/O system of C++ handles file operations which are very much similar to the console input and output operations. The input stream extracts data from the file and output stream inserts data to the file.

在这里插入图片描述

11.3 What is the difference between opening a file with a constructor function and opening a file with open() function? When is one method preferred over the other?

Answer:

When we open a file with a constructor, file name must be supplied during creation of object. Example:

ofstream outfile("result");

But when we use open() function we can provide file name later. Example:

ofstream outfile;
outfile.open("Result");

Another difference is we can provide file modes using open() function.

stream-object open("file name", mode);

Constructor method is preferred for only one file in the stream.

Member function open() of the class is preferred when we want to manage multiple files using one stream.

11.4 Explain how while(fin) statement detects the end-of-file that is connected to fin stream.

Answer:

The given statement is an ifstream object, such as fin, returns a value of 0 if any error occurs in the file operation including the end-of-file condition. We know while loop will terminate when fin returns zero. Thus end-of-file is detected.

11.5 What is a file mode? Describe the various file mode options available.

Answer:

在这里插入图片描述

11.6 How many file objects would you need to create to mange the following situations?

a. To process four files sequentially.

b. To merge two sorted files into a third file. Expalin.

Answer:

a. Only a single stream object.

b. In such case we need to create two seperate input stream for handling the two files and one output stream for handling the output file. That means require three stream objects.

11.7 What does the “current position” mean when applied to files?

Answer:

The “current position” applied to file means the byte location at which the next read or write operation will take place.

11.8 What are the advantages of saving data in binary form?

Answer:

The binary format is more accurate for storing the numbers as they are stored in the exact internal representation. Saving the data and therefore saving is much faster read() and write() function to do this work.

11.9 State whether the following statements are TRUE or FALSE.

a. A stream may be connected to more than one file at a time. (FALSE)

b. A file pointer always contains the address of the file. (FALSE - It can be a synonym for current position.)

c. The ios::ate mode allows us to write data anywhere in the file. (TRUE)

d. We can add data to an existing file by opening in write mode. (TRUE)

e. The parameter ios::app can be used only with the file capable of output. (TRUE)

f. The data written to a file with write() function can be read with the get() function. (FALSE)

g. Binary files store floating points values more accurately and compactly than the text files. (TRUE)

11.10 What is STL? How is it different from the C++ Standard Library? Why is it gaining importance among the programmers?

Answer:

The collection of generic classes and functions is called the standard template library (STL).

Using STL can save considerable time and effort and lead to high quality programs, thats why it is gaining importance among the programmers.

11.11 List the three types of containers.

Answer:

1. sequence containers.

2. Associative containers.

3. Derieved containers.

11.12 What is the major difference between a sequence container and an associative container?

Answer:

Sequence container store elements in a linear sequence. Associative containers store data in a structure called tree which facilities fast searching, deletion and insertion.

11.13 What are the best situations for the use of the sequence containers?

Answer:

When we need deal with linear data structures such as array, then we should use sequence container.

11.14 What are the best situations for the use of the associative containers?

Answer:

When we need non-linear containers that stores sets of values or key/value pair, then we should use associative container.

11.15 What is an iterator? What are its characteristics?

Answer:

Iterators is just likea pointer that are used to access container elements.

在这里插入图片描述

11.16 What is an algorithm? How STL algorithms are different from the conventional algorithms?

Answer:

Algorithm are functions that can be used generally across a variety of containers for processing their contents.

Conventional algorithms are very lengthy and complex, where STL algorithms are very easy and short that can save a lot of time and effort.

11.17 How are the STL algorithms implemented?

Answer:

Step 1: include in your program,

Step 2: Many algorithms operate om sequences of elements only indirectly through iterator.

Step 3: Now you can use STL algorithms. Such as find() to find the location of an element. STK provides you approximately 70 standard a;gorithms to save a lot of time and effort.

11.18 Distinguish between the following:

a. lists and vectors

Answer: vector supports random access, and a list can be accessed sequentially only.

b. sets and maps

Answer: set follows rapid lookup, while map follows key-based lookup.

c. maps and multimaps

Answer: A map allows only one key for a given value to be stored while multimap permits multiple keys.

d.queue and deque

Answer: In case of deque we can direct access to any element and in case of queue we follow first-in-first out.

e. arrays and vectors

Answer: size must be specify at compile time for array but size of vector is dunamically changed.

11.19 Compare the performance characteristics of the three sequence containers.

Answer:

在这里插入图片描述

11.20 Suggest appropriate containers for the following applications:

a. Insertion at the back of a container. (vector)

b. Frequent insertions and deletion at both the ends of a container. (deque)

c. Frequent insertions and deletions in the middle of a container. (list)

d. Frequent random access of elements. (vector)

11.21 State whether the following statements are true or false:

a. An iterator is a generalized form of pointer. (TRUE)

b. One purpose of an iterator is to connect algorithms to containers. (TRUE)

c. STL algorithms are member functions of containers. (FALSE)

d. The size of a vector does not change when its elements are removed. (FALSE)

Debugging Exercises:

11.4 Find erroes in the following statements.

a. ifstream.infile("DATA");

Answer: error. ifstream infile("DATA");

b. finl.getline(); // finl is input stream

Answer: Here you must give two arguments for getline() function, one is string type variable name and another is string size, such as:

char s[20]; getline(s, 20);

c. close(f1);

Answer: It is void type argument function so it will be fi.close();

d. infile.open(argc);

Answer: argc is called argument counter and argv is called argument vector so to open data file we can write the statement like this: infile.open(argv[1]);

e. sfinout.open(file, ios::in |ios::out| ios::ate);

Answer: The argument file must write as “file” because this is string type.

Programming Exercises:

11.1 Write a program that will create a data file containing the list of telephone numbers form as:

Marco 01282938

Chris 018273984

Use a class object to store each set of data.

Answer:

#include <iostream>
#include <fstream>
#define size 5
using namespace std;

class phone
{
    public:
        void set_data();
};

void phone:: set_data()
{
    ofstream Max("phone.txt");
    char *name[size] = {"Marco", "Chris", "Robin", "Oscar", "Stfanie"};
    char *number[size] = {"01282938", "018273984", "019837483", "0183734947", "018273648"};

    for(int i=0; i<size; i++)
    {
        Max.setf(ios::left, ios::adjustfield);
        Max.width(20);
        Max << name[i];

        Max.setf(ios::right, ios::adjustfield);
        Max.width(15);
        Max << number[i]<<endl;
    }
}

int main()
{
    phone book;
    book.set_data();

    return 0;
}

11.2 Write a program to read 11.1 “phone.txt” file and output the list in two columns. The name should be left justified and the numbers should be right justified.

Answer:

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;

int main()
{
    char name[50], n[50], number[50];

    ifstream Max("phone.txt");
    cout << "Enter your desire name to find mobile number : ";
    cin >> n;

    again:
    Max >> name;
    if(!strcmp(name, n))
    {
        Max.getline(number, 50);
        cout << setw(-20) << name << setw(25) << number << endl;
    }
    else
    {
        if(Max.eof()!=0)
            cout << "Sorry your input name is not found in list."<<endl;
        else
            goto again;
    }

    return 0;
}

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值