Hello Everyone,
This is my first post and I am really at the very beginner level of C++ (few days in) so lets see how this goes.
I wrote a script in PHP but I am now trying to convert it into C++ for the sake of learning. What I am having trouble with is converting PHP multi-dimensional associative arrays into C++ and being able to manage these arrays.
So, I will start with the original PHP code that I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
$selectedGenres = array(
'cartoons',
'bestSitcoms',
);
$genres = array(
'cartoons' => array(
'Archer',
),
'bestSitcoms' => array(
'Other Show',
),
'otherSitcoms' => array(
'Bad Show',
),
);
//method from another class that I am trying to replicate in c++
public function setShows($genres, $selectedGenres)
{
$this->shows = array();
foreach ($selectedGenres as $selectedGenre)
{
foreach ($genres[$selectedGenre] as $show)
{
$this->shows[] = $show;
}
}
}
|
So, what I am doing is setting up a list of "shows" and putting them into their appropriate genres (the $genres array). Then, I am taking a filter ($selectedGenres array) and am generating a new array of strings with just those shows. The method setShows() in this example would make the $this->shows array contain the strings "Archer" and "Other Show" but would leave out "Bad Show" because this genre is not in the filter of selectedGenres.
Now, that is the basic thing I am trying to do in C++. So far, I have gotten here to this point:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include "Classes/shows.cpp"
using namespace std;
int main()
{
shows showsClass;
vector<string> result;
string selectedGenres [] = {
"cartoons",
"bestSitcoms",
"otherSitcoms",
};
int selectedGenresSize = sizeof(selectedGenres) / sizeof(selectedGenres[0]);
map<string, map<int, string> > genres;
map<int, string> cartoons;
cartoons[0] = "Archer";
map<int, string> bestSitcoms;
bestSitcoms[0] = "Other Show";
map<int, string> otherSitcoms;
bestSitcoms[0] = "Bad Show";
genres["cartoons"] = cartoons;
genres["bestSitcoms"] = bestSitcoms;
showsClass.setShows(genres, selectedGenres, selectedGenresSize);
}
/** FILE Classes/shows.cpp below **/
#ifndef __SHOWS_H_INCLUDED__
#define __SHOWS_H_INCLUDED__
using namespace std;
class shows
{
protected:
string shows [];
public:
vector<string> setShows(map<string, map<int, string> > genres, string selectedGenres [], int selectedGenresSize)
{
vector<string> result;
map<string, map<int, string> >::iterator iter;
for (int i = 0; i < selectedGenresSize; i++)
{
cout << "+ " << selectedGenresSize << " + " << i << " + " << selectedGenres[i] << endl;
for (iter = genres.begin(); iter != genres.end(); ++iter)
{
if (selectedGenres[i] == iter->first)
{
cout << ". " << iter->first << endl;
}
}
}
return result;
/*foreach ($selectedGenres as $selectedGenre)
{
foreach ($genres[$selectedGenre] as $show)
{
$this->shows[] = $show;
}
}*/
}
};
#endif
|
Now, what the point this has gotten me to is the ability to use the filter to somewhat get the map of genres to use the filter, but I don't know how to get a list of items within this map. So, I can get it to say "this iteration of the map is good" but I can't get "add the values stored within this iteration to the result vector".
So, my questions are:
1) How can I get the string values stored in the map iteration out and add them to the result vector?
2) Am I doing things correctly or is this just some really bad code?
Any advice or comments, no matter how harsh, is more than welcome.