โ† All CPP Flashcard Decks

File Handling & Standard Template Library (STL) Flashcards

9 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 9 File Handling & Standard Template Library (STL) flashcards as text
  1. Which header file is required for file handling in C++?

    Answer:

    The `` header file in C++ provides classes for file stream operations, including `ifstream` for input from files, `ofstream` for output to files, and `fstream` for both. It is essential for performing various file I/O operations in C++.

  2. Which stream is used to write to a file?

    Answer: ofstream

    `ofstream` (output file stream) is the class specifically designed for writing data to files in C++. You create an `ofstream` object, associate it with a file, and then use stream insertion operators (`<<`) to write data to that file.

  3. Which STL container stores key-value pairs?

    Answer: map

    The `std::map` container in the C++ Standard Template Library (STL) stores elements as key-value pairs, where each key is unique and associated with a specific value. It provides efficient lookup, insertion, and deletion operations based on the keys, maintaining sorted order.

  4. Which operation checks if a file stream opened successfully?

    Answer: file.is_open()

    The `is_open()` member function of file stream objects (like `ifstream`, `ofstream`, `fstream`) is used to check if the file stream successfully opened a file. It returns `true` if the file is open and ready for I/O operations, and `false` otherwise, allowing for error handling.

  5. Which STL container allows access in constant time using indices?

    Answer: vector

    `std::vector` is a dynamic array in the C++ STL that provides constant-time (O(1)) access to its elements using their integer indices. This efficiency is due to elements being stored contiguously in memory, allowing direct calculation of an element's address.

  6. Which function is used to read data from a file line by line?

    Answer: getline()

    The `std::getline()` function is used to read an entire line from an input stream (like a file stream or `std::cin`) until a newline character is encountered or the end of the stream is reached. It stores the read line into a `std::string` object, making it ideal for line-by-line file processing.

  7. Which STL algorithm is used to sort elements?

    Answer: sort()

    The `std::sort()` algorithm from the `` header is a powerful and efficient function in the C++ STL used to sort elements within a specified range. It can sort various container types and allows for custom comparison functions to define specific sorting criteria.

  8. What is required to use STL containers like vector or map?

    Answer: #include

    To use specific STL containers like `std::vector`, `std::map`, `std::list`, etc., you must include their respective header files. For `std::vector`, the correct header is ``, which provides the necessary class definition and associated functionalities.

  9. Which stream is used to read from a file?

    Answer: ifstream

    `ifstream` (input file stream) is the class specifically designed for reading data from files in C++. You create an `ifstream` object, associate it with a file, and then use stream extraction operators (`>>`) or `getline()` to read data from that file.