Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Crow unordered_map Quiz
//clang 3.8.0 #include <iostream> #include <unordered_map> #include <string> int main() { //Create map that has string keys and double values std::unordered_map<std::string, double> grades; //assign values to keys //format: unordered_map[key] = value grades["Project 1"] = 94.5; grades["Quiz 1"] = 63.75; grades["Homework 1"] = 100; //pulling a specific value from the map std::cout << "---Showing how to retrieve a value" << std::endl; std::string assignment = "Project 1"; std::cout << "You got a " << grades[assignment] << " on " << assignment << std::endl; //detecting if an item is in the map std::cout << "---Showing how to check if a key exists" << std::endl; assignment = "Project 2"; bool hasKey = !(grades.find(assignment) == grades.end()); std::cout << "You have received a grade on " << assignment << ": " << hasKey << std::endl; //There is no "Project 2" key //Looping through the map std::cout << "---Showing how to print all values" << std::endl; double sum = 0; auto it = grades.begin(); for(; it != grades.end(); it++) { std::cout << "You received a " << (it->second) << " on " << (it->first) << std::endl; sum += it->second; } std::cout << "You got a " << (sum/grades.size()) << " average." << std::endl; }
run
|
edit
|
history
|
help
0
Linker error while taking the address of a constexpr variable
Range List for C++
Thread-safe Interval Average Calculator
Access to temporary object
template specialization inheritance problem
Alternation regex
mddd
Atomic trivial default constructor
function returning a function demo
Pointer array