W6: Intro to the Standard Library Flashcards

1
Q

What component of the standard library is not declared in the std namespace?

A

The filesystem component

namespace filesystem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The string library provides support for three types of strings:

A

the string classes

the string_view classes

null terminated C-style string functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String class specializations:

A

std: :string (>)
std: :wstring (>)
std: :u16string >)
std: :u32string >)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

String class public member functions:

operator=

operator[]

size()

substr()

operator+=

A
  • assigns a string to the current string
  • accesses a specified character in the string
  • the number of characters in the string
  • returns a substring of the string
  • append a character or a string to the current string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

String class public member functions:

find(char c)
rfind(char c)
find_first_of(char c)
find_last_of(char c)
find_first_of(basic_string_view(const CharT* s))
find_last_of(basic_string_view(const CharT* s))

A
  • find the first occurence of c in the string
  • find the last occurence of c in the string
  • find the first occurence of c in the string
  • find the last occurence of c in the string
  • find the first occurence of any character in the sequence s
  • find the last occurence of any character in the sequence s
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

a string type keeps track of the number of characters in the string through an instance variable without using the null byte as a terminator. A string object can include one or more null byte (‘\0’) characters.

What is the output:

 #include 
 #include 
 int main() {
     std::string str("Hello");
     str += '\0'; // adds a null byte
     str += " World"; // adds a string
     int i = 0;
     for (const auto& c : str)
         if (str[i++] == '\0')
             std::cout << "Null byte at str["
              << i-1 << "]\n"; 
     std::cout << str << std::endl;
 }
A

Null byte at str[5]

Hello World

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

string_view library characteristics:

A
  • avoids redundant copying of strings
  • does not own the string, read only
  • read only view into a contiguous sequence of characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

string_view class specializations

A

std: :string_view (>)
std: :wstring_view (>)
std: :u16string_view >)
std: :u32string_view >)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What operators does the string_view class omit from the string class?

A

operator +=

operator&raquo_space;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A complete programming solution to the implementation of a data structure requires:

A

the definition of the data type of each element in the data structure

the choice of the optimal data structure to collect the elements

the function object for the algorithm to use on the data structure

syntax to access the facilities of the STL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

lvalue and rvalue Distinction

A

std: :ref() () - returns an lvalue reference to its argument
std: :move() () - returns an rvalue reference to its argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

C++17 introduced new library facilities. They include:

A

std::string_view () - a read-only contiguous sequence of characters (see above)
std::variant() () - represents a type-safe union
std::optional () - may or may not contain a value (beyond scope)
std::any () - single values of any type (beyond scope)
std::uncaught_exceptions () - stack is in the process of unwinding
Uniform container access std::size std::empty std::data - number of elements, emptiness, direct access to underlying array
Special Mathematical Functions () -= (beyond scope)
Filesystem Library filesystem:: () - facilities for performing operations on file systems and their components including paths, regular files and directories (beyond scope)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

class template std::variant represents:

A

a type-safe union

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The value of a variant can be accessed by

A

using its type if it’s unique

using its index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

variant class template member functions include:

A

operator=(v) - assigns value of variant v to the left operand
index() - returns the index I of the variant stored in the current object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

variant class template helper functions include:

A

std: :get(v)- returns value of variant v given the type T
std: :get<i>(v) - returns value of variant v given the index I</i>

17
Q

variant class template helper classes include:

A

std::bad_variant_access - exception thrown on invalid access

18
Q

What will be the output?

A
#include 
 #include 
 int main() {
     std::variant a, b;
     a = 12l; // a contains a long
     b = std::get(a);
     std::cout << b << std::endl;
     try {
         double c = std::get(b);
         std::cout << c << std::endl;
     }
     catch(std::bad_variant_access& bva) {
         std::cout << "bad type access" << std::endl; 
     }
 }