Solution requirements:
- Pre C++11
Statements:
- Function
someFunctionexists, that accepts 2D vector as vector of string vectors:vector2d. - Class
someClassexists, that contains 1D vector of strings:parameters. - Vector of objects of aformentioned Class is created:
vectorOfClassObjects.
Problem definition:
It is possible to feed 1D vector parameter of class objects vector vectorOfClassObjects into someFunction as 2D vector? Can vector of strings, of vector of class objects be interpreted as function parameter of 2D vector of strings?
Minimum reproducible example:
#include <windows.h>
#include <iostream>
#include <cstring>
#include <vector>
class someClass{
public:
std::vector<std::string> parameters;
};
void someFunction(std::vector<std::vector<std::string>> vector2d){
for(int i = 0; i < vector2d.size(); i++){
MessageBox(NULL, vector2d[i][0].c_str(), "", MB_OK);
}
}
int main()
{
// Execution #1 - expected output of Execution #2
someFunction({{"A", "a"}, {"B", "b"}});
// Execution #2
std::vector<someClass> vectorOfClassObjects;
vectorOfClassObjects.push_back(someClass());
vectorOfClassObjects[0].parameters.push_back("C");
vectorOfClassObjects[0].parameters.push_back("c");
vectorOfClassObjects.push_back(someClass());
vectorOfClassObjects[1].parameters.push_back("D");
vectorOfClassObjects[1].parameters.push_back("d");
//It is possible to feed that? What syntax?
someFunction(?vectorOfClassObjects.parameters?);
return 0;
}
someFunction({{"A", "a"}, {"B", "b"}});- this will not compile pre C++11#1you have a vector with 2 inner vectors each with 1 element. Thats not going to fly without creating such a vector (by copying the elements)