#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <map>
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef struct tagFoo {
int a;
int b;
} Foo;
int main() {
Foo a;
map<const char*, Foo, ltstr>::iterator iter;
map<const char*, Foo, ltstr> M;
//isi dengan data dummy
a.a = 10;
a.b = 20;
M["1"]=a;
a.a = 30;
a.b = 40;
M["10"]=a;
//print elemen
int i;
for (iter = M.begin(), i = 0; i < M.size(); i++, iter++) {
printf("M[%s] a=%2d b=%2d\n", iter->first, M[iter->first].a,
M[iter->first].b);
}
return 0;
}
Bisa nggak ya kita iterasi seluruh elemen M tanpa menggunakan variable i?
cukup dengan iter.
--
Utk berhenti langganan, kirim email ke [EMAIL PROTECTED]
Informasi arsip di http://www.linux.or.id/milis.php3