C++ Containers

  Home  C++ Programming  C++ Containers


“C++ Containers frequently Asked Questions by expert members with experience in C++ Containers. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



19 C++ Containers Questions And Answers

1⟩ What is Iterator class?

An iterator class is used to iterate through objects of the container class. An iterator is an entity that gives access to the contents of a container object.

 204 views

2⟩ Tell us what you know about Iterator class?

A container class hold group of objects and iterator class is used to traverse through the objects maintained by a container class. The iterator class provides access to the classes inside a container. They are objects that point to other objects. Iterator points to one element in a range, and then it is possible to increment it so that it points to the next element.

There are several different types of iterators:

input_iterator

output_iterator

forward_iterator

bidirectional_iterator

random_iterator

reverse_iterator

 200 views

7⟩ What is Input Iterator?

These iterators can read values in the forward movement. They can be incremented, compared and dereferenced.

 218 views

8⟩ Explain containers of pointer?

Container class is one of the classes that were put into class libraries. To handle objects that contain other objects, container classes are used. A GUI class library contains a group of container classes.

Containers of pointers provide containers to hold the objects that are heap-allocated in manner that is exception-safe and with minimum overhead. The central idea is to make OOP easier in C++. This is done by establishing a standard a set of classes, methods to deal with OO specific problems.

 214 views

9⟩ What are the C++ standardized container classes?

The following are the standardized container classes:

std::map: Used for handle sparse array or a sparse matrix.

std::vector: Like an array, this standard container class offers additional features such as bunds checking through the at () member function, inserting or removing elements, automatic memory management and throwing exceptions.

std::string: A better supplement for arrays of chars.

 210 views

10⟩ What are storage classes?

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program.

 212 views

11⟩ What are External variable?

External variable are defined outside any function and memory is set aside for this type of variable once it is declared and remained until the end of the program. These variables are also called global variables.

 233 views

13⟩ What are static automatic variables?

The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

 202 views

14⟩ What is associative container?

Associative containers provide lookup based on keys. They come in two variants:

• Ordered associative containers do lookup based on an ordering criterion, by default < (less

than). They are implemented as balanced binary trees, usually red-black trees.

• Unordered associative containers do lookup based on a hash function. They are implemented

as hash tables with linked overflow.

Both come as

• maps: sequences of {key,value} pairs

• sets: maps without values (or you could say that the key is also the value)

Finally, maps and sets, whether ordered or unordered, come in two variants:

• ‘‘Plain’’ sets or maps with a unique entry for each key

• ‘‘Multi’’ sets or maps for which multiple entries can exist for each key

The name of an associate container indicates its place in this 3-dimensional space: {set|map,

plain|unordered, plain|multi}. ‘‘Plain’’ is nev er spelled out, so the associative containers are:

Associative Containers (§iso.23.4.1, §iso.23.5.1)

set multiset unordered_set unordered_multiset

map multimap unordered_map unordered_multimap

Their template arguments are described in §31.4.

Internally, a map and an unordered_map are very different. See §31.2.1 for graphical representations.

In particular, map uses its comparison criterion (typically <) on a key to search through a balanced tree (an O(log(n)) operation), whereas unordered_map applies a hash function on a key to find a slot in a hash table (an O(1) operation for a good hash function).

 209 views

15⟩ Explain different types of iterators, i.e. input_iterator, output_iterator etc?

Input Iterator: These iterators can read values in the forward movement. They can be incremented, compared and dereferenced.

Ouptut Iterator: They write values in the forward movement. They can be incremented and dereferenced.

Forward Iterator: They are like input and output iterators that can read and write values in forward movement.

Bidirectional iterators: These can Read and write values with forward and backward movement and can be incremented, decremented.

random_access_iterator: Can read and write values randomly.

 193 views

16⟩ Tell me when should we use container classes instead of arrays?

It is advisable to use container classes of the STL so that you don’t have to go through the pain of writing the entire code for handling collisions, making sure its working well, testing it repeatedly with an overhead of time consumption.

Suppose you have some data that has values associated with strings and its fields consist of grades> in this situation you can directly use hash table instead of having it created by yourself.

 193 views

17⟩ What is Automatic variable?

Automatic variable, also called as local variable and it has scope only within the function block where it is defined.

 215 views

18⟩ Do you know what is a container class? What are the types of container classes?

A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. A generic class plays a role of generic holder. A container class is a good blend of predefined behavior and an interface that is well known. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory. A container class is known as heterogeneous container, when it contains a set of different objects. A container class is known as homogeneous container when it contains a set of similar objects.

 192 views

19⟩ What is a container class? What are the types of container classes?

A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. A generic class plays a role of generic holder. A container class is a good blend of predefined behavior and an interface that is well known. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory. A container class is known as heterogeneous container, when it contains a set of different objects. A container class is known as homogeneous container when it contains a set of similar objects.

 201 views