題目如題,想實作名表創建及查詢
想問下怎么改才能實作
代碼如下:
#include <iostream>
#include <string>
#include <cstring>
#include <map>
#include <cstdlib>
#include <cstddef>
using namespace std;
class table_item
{
int num;
int a[8];
public:
table_item()
{
num = 0;
}
table_item( int n, int* p)
{
num = n;
for (int i = 0; i < 8; i++, p++)
a[i] = *p;
}
table_item(table_item& y)
{
num = y.num;
memcpy(a, y.a, 32);
}
void display()
{
cout << "num:" << num <<" "<< "score:";
for (int i = 0; i < 8; i++)
cout << a[i] << " ";
cout << "\n";
}
};
int main()
{
map<string, table_item> namelist;
cout << "請打出您想輸入的個數" << endl;
int n; cin >> n;
int i;
cout << "請輸入姓名,學號,語數英物化生史地成績,以空格間隔" << endl;
for (i = 0; i < n; i++)
{
string x;
cin >> x;
int no;
cin >> no;
int b[8];
for (int j = 0; j < 8; j++)
cin >> b[j];
int* q = b;
table_item sample( no, q);
namelist[x] = sample;
}
cout << "您的資料已成功存入資料庫!" << endl;
cout << "請輸入您想查找的學生姓名:" << endl;
string search;
cin >> search;
map<string, table_item> ::const_iterator it;
bool samename(string x);
it = find_if(namelist.begin(), namelist.end(), samename(search));
if (it == namelist.end()) cout << "您想查找的資訊不在資料庫中" << endl;
else
{
cout << "姓名:" << it->first << " ";
((*it).second).display();
}
}
bool samename(string x, iterator it)
{
return it->first == x;
}
uj5u.com熱心網友回復:
別的地方也有點小錯,望指正uj5u.com熱心網友回復:
沒人嗎
uj5u.com熱心網友回復:
你可以定義一個迭代器 it ,it->second.displayuj5u.com熱心網友回復:
map<string, table_item>::iterator iter = namelist.begin();iter->second.display();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78795.html
標籤:C++ 語言
上一篇:看了27行,沒有找到錯誤的地方啊
