二维数组的问题,帮帮忙
#include <map>
#include <string>
#include <iostream>
using namespace std;
map <string, string> convert_map;
typedef const char * pcc;
void load_convert_table( pcc ** convert );
pcc convert[13][2] = {
{ "图片.体育明星 ", "sport "},
{ "图片.女明星 ", "women "},
{ "图片.军事 ", "military "},
{ "图片.动漫 ", "cartoon "},
{ "图片.名人 ", "person "},
{ "图片.男明星 ", "men "},
{ "图片.搞笑 ", "humor "},
{ "图片.科教 ", "science "},
{ "图片.风景 ", "view "},
{ "图片.名车 ", "car "},
{ "图片.游戏 ", "game "},
{ "图片.生物 ", "bio "},
{ "图片.其他 ", "movie "}
};
int main()
{
load_convert_table( reinterpret_cast <pcc **> (convert) );
map <string, string> ::iterator it = convert_map.begin();
for( ; it != convert_map.end(); it++ )
cout < <it-> first < < " " < <it-> second < <endl;
}
void load_convert_table( pcc ** convert )
{
while( convert != NULL )
{
convert_map.insert(make_pair(*convert[1],*convert[2]));
convert++;
}
}
[解决办法]
这个题目的确有点麻烦,20分要不都给我就对不起我研究了半个小时了,杀死脑细胞无数;
最明显的错误:make_pair(*convert[1],*convert[2])应该是make_pair(*convert[0],*convert[1])才对。指针的传递也是错误的,导致convert[0]的值为未知值,这时候再*肯定出错(程序中断).
下面是我的正确代码:
#include "stdafx.h "
#include <map>
#include <string>
#include <iostream>
using namespace std;
map <string, string> convert_map;
typedef const char * pcc;
void load_convert_table( pcc *convert);
pcc convert[14][2] = {
{ "图片.体育明星 ", "sport "},
{ "图片.女明星 ", "women "},
{ "图片.军事 ", "military "},
{ "图片.动漫 ", "cartoon "},
{ "图片.名人 ", "person "},
{ "图片.男明星 ", "men "},
{ "图片.搞笑 ", "humor "},
{ "图片.科教 ", "science "},
{ "图片.风景 ", "view "},
{ "图片.名车 ", "car "},
{ "图片.游戏 ", "game "},
{ "图片.生物 ", "bio "},
{ "图片.其他 ", "movie "}/*,
{NULL, NULL}*/
};
int main()
{
load_convert_table( (pcc*)convert);
map <string, string> ::iterator it = convert_map.begin();
for( ; it != convert_map.end(); it++ )
cout < <it-> first < < " " < <it-> second < <endl;
system( "pause ");
}
void load_convert_table( pcc * convert )
{
pcc *a = NULL, *b = NULL;
while( convert != NULL && *convert != NULL)
{
a = convert++;
b = convert++;
convert_map.insert(make_pair(*a, *b));
}
}