array question

in php i can do an array like this

1
2
3
4
5
6
7
8
9
10
11
12
Array
(
    [work] => Array
        (
                    [name] => carl
                    [office] => 3952
                    [home] => 81279319

        )

   
)


can I do the same in c++ where I use the work as an identifier?

You are trying to say work is an array. Inside this array, there are three elements referenced by name. Each element contain an entry.

In C++ array which reference it's element by index are usually called array or vector or queue, deque etc. Array which reference it's element by name are usually called map, multimap etc.

map<string, string> work should be what you want.

thanks will check this out
Actually PHP was created long after C++ exist so a lot of features C++ have it and PHP just copy the concept over.

What I don't like about PHP is they don't differentiate reference using index or name. E.g
work[0] is array which reference it's element by index. work['name'] which reference it's element by name. The syntax is too "small" for one to notice work is what variable datatype.

In this aspect, Perl is better. @work indicate array which reference it's element by index. %work indicate hash aka associative array which reference it's elements by name.
yea i get what u mean on PHP.....it has been made too simple for the developer to use......

that is why I had the question on how this is implemented in c++
Hi Carlsum1986,
If I am not mistaken, you would like to define several elements of type "work" each having a name, office and home element, right? If that is the case, a map or multimap would not be an ideal C++ type. I would suggest you to declare a class type or a struct type to encapsulate the work elements and then create a vector of those work elements like the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
using namespace std;
class work{
public:
	string m_sName;
	string m_sOffice;
	string m_sHome;
	work(const string& sName, const string& sOffice, const string& sHome):m_sName(sName), m_sOffice(sOffice), m_sHome(sHome){}
};
int main(int argc, char* argv[])
{
	vector<work> v;
	v.push_back(work("w1", "1111", "101010"));
	v.push_back(work("w2", "2222", "202020"));
	for(vector<work>::iterator vit = v.begin(); vit != v.end(); ++vit)
		cout << vit->m_sName << ", " << vit->m_sOffice << ", " << vit->m_sHome << endl;
}


On the other hand, if you just want to associate few keywords with values, sohguanh is right & you should use either a map or multimap (if you want to associate different values with the same keyword).
Topic archived. No new replies allowed.