In php when you want to create array, by using this:
$Data[] = "Val1";
$Data[] = "Val1";
$Data[] = "Val1";
php set auto key 0, 1, 2 for this array. I want to know what should I do in c++ in map types or other array types to set int keys auto from 0 to unlimited?
In c++ is something like this:
map<int, string> Data;
Data[0] = "val1";
Data[1] = "val1";
Data[2] = "val1";
I have to set key myself!
Is there any way to create struct or template and using that with map?
IIUC, you want to use vector:
#include <vector>
#include <string>
...
using namespace std;
...
vector<string> Data;
Data.push_back("val1");
Data.push_back("val1");
Data.push_back("val1");
// Now you can access the inserted elements via Data[i]
See a full example to using this container on this page.
You can use variable:
map<int, string> Data;
int counter = 0;
Data[counter++] = "val1";
Data[counter++] = "val1";
Data[counter++] = "val1";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With