Complete the code to compare a character variable with a character literal.
char ch = 'a'; if (ch == [1]) { std::cout << "Match" << std::endl; }
Characters are compared using single quotes, like 'a'. Double quotes denote strings.
Complete the code to compare a std::string variable with a string literal.
#include <string> std::string s = "hello"; if (s == [1]) { std::cout << "Equal" << std::endl; }
Strings are compared using double quotes, like "hello". Single quotes are for characters.
Fix the error in the code comparing a char and a string literal.
char ch = 'x'; if (ch == [1]) { std::cout << "Yes" << std::endl; }
Characters must be compared with single quotes, not double quotes which denote strings.
Fill both blanks to create a map with string keys and char values, filtering keys starting with 'a'.
#include <map> #include <string> #include <iostream> std::map<std::string, char> filtered = { {"apple", 'a'}, {"banana", 'b'}, {"apricot", 'a'} }; std::map<std::string, char> result = { {k, v} for auto& [k, v] : filtered if k[1] [2] 'a' };
To check the first character of a string, use indexing with [0]. To filter keys starting with 'a', use == 'a'.
Fill all three blanks to create a map of uppercase keys with values greater than 10.
#include <map> #include <string> #include <cctype> std::map<std::string, int> data = { {"one", 1}, {"two", 20}, {"three", 30} }; std::map<std::string, int> filtered = { [1]: v for (const auto& [k, v] : data) if v [2] 10 }; for (const auto& [k, v] : filtered) { std::cout << k << ": " << v << std::endl; }
To create uppercase keys, transform the first character with std::toupper and append the rest with substr. Filter values greater than 10 using >. Use the original value v in the map.