auto x = 42; // int
auto y = 3.14; // double
auto s = string("hi"); // string
// Especially useful with complex types:
map<string, vector<int>>::iterator it = data.begin(); // old way
auto it = data.begin(); // new way!
vector<int> nums = {1,2,3,4,5};
for (int x : nums) cout << x; // by value (copy)
for (const int& x : nums) cout << x; // by const ref (cheap)
for (int& x : nums) x *= 2; // by ref (modifies)
map<string,int> scores = {{"Alice",95},{"Bob",87}};
for (auto& [name, score] : scores) {
cout << name << ": " << score;
}
Write filterPositive(vector<int> v) using range-based for that returns only elements > 0.
Click "Run" to execute your code.