#include <iostream>
#include <string>
#include <vector>
#include <map>
void printVector(std::vector<std::string>& vec) {
// range based loop
for (auto& text : vec) {
std::cout << text << "\n";
}
std::cout << "\n";
}
void setup() {
Serial.begin(115200);
// syntax
std::vector<std::string> colour {"Blue", "Red", "Orange"};
// Strings can be added at any time
// with push_back
colour.push_back("Yellow");
// Print Strings stored in Vector
for (size_t i {0}; i < colour.size(); i++) {
std::cout << colour.at(i) << "\n";
}
std::cout << "\n";
printVector(colour);
// 2D Array
std::vector<std::vector<std::string>> dArr {
{"Hallo", "Welt"},
{"Arduino", "Fan" }
};
try {
// Two different ways to access the index
std::cout << dArr[0][0] + " " + dArr[0][1] << "\n"; // classic
std::cout << dArr.at(1).at(0) << " " << dArr.at(1).at(2) << "\n\n"; // Safety: Error at(2) is out of range (catched)
} catch (const std::out_of_range& ex) { std::cout << ex.what() << '\n'; }
// Key : Value
std::map<std::string, int> m {
{"CPU", 10},
{"GPU", 15},
{"RAM", 20}
};
std::cout << "CPU: " << m["CPU"] << " GPU: " << m["GPU"] << " RAM: " << m["RAM"] << "\n" << std::endl;
// Old Style
const char* feldText[2][2] {
{"Hallo", "Welt"},
{"Arduino", "Fan" }
};
std::cout << feldText[0][0] << " " << feldText[0][1] << "\n";
std::cout << feldText[1][0] << " " << feldText[1][1] << "\n";
}
void loop() {}