#include <string> #include <iostream> #include <variant> struct SampleVisitor { void operator()(int i) const { std::cout << "int: " << i << "\n"; } void operator()(float f) const { std::cout << "float: " << f << "\n"; } void operator()(const std::string& s) const { std::cout << "string: " << s << "\n"; } }; int main() { std::variant<int, float, std::string> intFloatString; static_assert(std::variant_size_v<decltype(intFloatString)> == 3); // default initialized to the first alternative, should be 0 std::visit(SampleVisitor{}, intFloatString); // index will show the currently used 'type' std::cout << "index = " << intFloatString.index() << std::endl; intFloatString = 100.0f; std::cout << "index = " << intFloatString.index() << std::endl; intFloatString = "hello super world"; std::cout << "index = " << intFloatString.index() << std::endl; // try with get_if: if (const auto intPtr (std::get_if<int>(&intFloatString)); intPtr) std::cout << "int!" << *intPtr << "\n"; else if (const auto floatPtr (std::get_if<float>(&intFloatString)); floatPtr) std::cout << "float!" << *floatPtr << "\n"; if (std::holds_alternative<int>(intFloatString)) std::cout << "the variant holds an int!\n"; else if (std::holds_alternative<float>(intFloatString)) std::cout << "the variant holds a float\n"; else if (std::holds_alternative<std::string>(intFloatString)) std::cout << "the variant holds a string\n"; // try/catch and bad_variant_access try { auto f = std::get<float>(intFloatString); std::cout << "float! " << f << "\n"; } catch (std::bad_variant_access&) { std::cout << "our variant doesn't hold float at this moment...\n"; } // visit: std::visit(SampleVisitor{}, intFloatString); intFloatString = 10; std::visit(SampleVisitor{}, intFloatString); intFloatString = 10.0f; std::visit(SampleVisitor{}, intFloatString); }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}