在C中,map是典型的关联容器或者叫映射容器(associative container),其中的每一个元素都是由key-value这样成对出现的内容组成的,比如学号和学生之类具有一一对应关系的情形,学号可以作为key,学生对象可以作为key所对应的value。很显然这种情况下的key只
在C++中,map是典型的关联容器或者叫映射容器(associative container),其中的每一个元素都是由key-value这样成对出现的内容组成的,比如学号和学生之类具有一一对应关系的情形,学号可以作为key,学生对象可以作为key所对应的value。很显然这种情况下的key只有一个值,但是,在实际工作中,我们可能会经常需要使用多个值组合起来作为key的情况,比如我们要按照学生的视力和身高进行排序,以决定学生的座位排在前面还是后面,而且还是假定要用map来解决这样的问题(当然,这样的问题有很多其它的解决办法),那应该怎么办呢?
(1) 单值作为key的情形
我们知道map在缺省状态下,其数据是按照key的升序进行排列的。假定我们有一个Student类,声明如下:
[cpp] view plaincopy
- class Student
- {
- private:
- int id; // 学号
- string name; // 姓名
- float eyesight; // 视力
- float height; // 身高
- float chinese; // 语文成绩
- float english; // 英文成绩
- float math; // 数学成绩
- public:
- Student(int id, string name,floateyesight, float height,float chinese, float english,float math)
- {
- this->id = id;
- this->name = name;
- this->eyesight = eyesight;
- this->height = height;
- this->chinese = chinese;
- this->english = english;
- this->math = math;
- }
- int get_id()
- {
- return id;
- }
- string get_name()
- {
- return name;
- }
- float get_eyesight()
- {
- return eyesight;
- }
- float get_height()
- {
- return height;
- }
- float get_chinese()
- {
- return chinese;
- }
- float get_english()
- {
- return english;
- }
- float get_math()
- {
- return math;
- }
- };
那么下面的程序:
[cpp] view plaincopy
- int main(int argc,char**argv)
- {
- mapint, Student> stu_map; // int作为key的类型,Student作为value的类型
- stu_map.insert(make_pair(4,Student(4, "Dudley",1.1f, 170.2f, 90.5f, 89.5f, 93.0)));
- stu_map.insert(make_pair(3,Student(3, "Chris", 1.1f, 163.4f, 93.5f,90.0f, 83.5f)));
- stu_map.insert(make_pair(2,Student(2, "Bob", 1.5f, 166.6f, 86.0f,98.5f, 85.0f)));
- stu_map.insert(make_pair(1,Student(1, "Andrew", 1.5f, 173.2f, 98.5f,100.0f, 100.f)));
- mapint, Student>::iterator iter;
- for(iter = stu_map.begin(); iter != stu_map.end();++iter)
- {
- coutfirst "\t"second.get_name()
- }
- return 0;
- }
就会按照学号的升序给出输出:
1 Andrew
2 Bob
3 Chris
4 Dudley
这是缺省的情形,如果要将学生的姓名按照学号的降序输出,那么仅需将上面main函数中的
mapint,Student> stu_map;改为
mapint,Student, greaterint> > stu_map;
以及
mapint,Student>::iterator iter;改为
mapint,Student, greaterint> >::iteratoriter;
即可。
其实,mapint,Student> stu_map;这是一种缺省的情况,它和
mapint,Student, lessint> > stu_map;是一样的。
(2) 多值组合作为key的情形
现在,我们来看看多个值组合起来作为key的情况,为此,我们需要为key定义一个类,如下:
[cpp] view plaincopy
- class key
- {
- public:
- float eyesight;
- float height;
- key(float x, floaty):eyesight(x), height(y)
- {
- }
- friend bool operator const key&);
- };
- bool operator const key& key2)
- {
- // 按eyesight升序 + height升序排列
- if(key1.eyesight != key2.eyesight)
- return (key1.eyesight
- else
- return (key1.height
- // 按eyesight降序 + height降序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight > key2.eyesight);
- //else
- // return(key1.height > key2.height);
- // 按eyesight升序 + height降序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight
- //else
- // return(key1.height > key2.height);
- // 按eyesight降序 + height升序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight > key2.eyesight);
- //else
- // return(key1.height
- }
再修改main函数如下:
[cpp] view plaincopy
- int main(int argc,char**argv)
- {
-
map
stu_map; - Studentstu4(4, "Dudley",1.1f, 170.2f, 90.5f, 89.5f, 93.0);
- Studentstu3(3, "Chris", 1.1f, 163.4f, 93.5f,90.0f, 83.5f);
- Studentstu2(2, "Bob", 1.5f, 166.6f, 86.0f,98.5f, 85.0f);
- Studentstu1(1, "Andrew", 1.5f, 173.2f, 98.5f,100.0f, 100.f);
- stu_map.insert(make_pair(key(stu4.get_eyesight(),stu4.get_height()), stu4));
- stu_map.insert(make_pair(key(stu3.get_eyesight(),stu3.get_height()), stu3));
- stu_map.insert(make_pair(key(stu2.get_eyesight(),stu2.get_height()), stu2));
- stu_map.insert(make_pair(key(stu1.get_eyesight(),stu1.get_height()), stu1));
-
map
::iterator iter; - for(iter = stu_map.begin(); iter != stu_map.end();++iter)
- {
- coutfirst.eyesight "\t"first.height "\t" second.get_id()"\t" second.get_name()
- }
- return 0;
- }
那么输出结果为:
1.1 163.4 3 Chris
1.1 170.2 4 Dudley
1.5 166.6 2 Bob
1.5 173.2 1 Andrew
从上面的输出,我们可以很明显地看到,是按照视力升序和升高升序输出的,另外三种可能的排序情况,也在类key的操作符“
(3)结论
1.通常我们不用STL algorithm中的sort函数,来对一个map进行排序,而对vector的元素进行排序则可以很方面地使用sort函数;
2.多值组合作为key的情况,需要我们自己定义一个key类,并在该类中重载操作符“
附两个值作为key的情况之完整的实验代码如下:
[cpp] view plaincopy
-
#include
- #include
-
#include
- using namespace std;
- class key
- {
- public:
- float eyesight;
- float height;
- key(float x, floaty):eyesight(x), height(y)
- {
- }
- friend bool operator const key&);
- };
- bool operator const key& key2)
- {
- // 按eyesight升序 + height升序排列
- if(key1.eyesight != key2.eyesight)
- return (key1.eyesight
- else
- return (key1.height
- // 按eyesight降序 + height降序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight > key2.eyesight);
- //else
- // return(key1.height > key2.height);
- // 按eyesight升序 + height降序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight
- //else
- // return(key1.height > key2.height);
- // 按eyesight降序 + height升序排列
- //if(key1.eyesight != key2.eyesight)
- // return(key1.eyesight > key2.eyesight);
- //else
- // return(key1.height
- }
- class Student
- {
- private:
- int id; //学号
- string name; // 姓名
- float eyesight; //视力
- float height; //身高
- float chinese; //语文成绩
- float english; //英文成绩
- float math; //数学成绩
- public:
- Student(int id, string name,floateyesight,float height,float chinese,float english,float math)
- {
- this->id = id;
- this->name = name;
- this->eyesight = eyesight;
- this->height = height;
- this->chinese = chinese;
- this->english = english;
- this->math = math;
- }
- int get_id()
- {
- return id;
- }
- string get_name()
- {
- return name;
- }
- float get_eyesight()
- {
- return eyesight;
- }
- float get_height()
- {
- return height;
- }
- float get_chinese()
- {
- return chinese;
- }
- float get_english()
- {
- return english;
- }
- float get_math()
- {
- return math;
- }
- };
- int main(int argc,char**argv)
- {
-
map
stu_map; - Studentstu4(4, "Dudley",1.1f, 170.2f, 90.5f, 89.5f, 93.0);
- Studentstu3(3, "Chris", 1.1f, 163.4f, 93.5f,90.0f, 83.5f);
- Studentstu2(2, "Bob", 1.5f, 166.6f, 86.0f,98.5f, 85.0f);
- Studentstu1(1, "Andrew", 1.5f, 173.2f, 98.5f,100.0f, 100.f);
- stu_map.insert(make_pair(key(stu4.get_eyesight(),stu4.get_height()), stu4));
- stu_map.insert(make_pair(key(stu3.get_eyesight(),stu3.get_height()), stu3));
- stu_map.insert(make_pair(key(stu2.get_eyesight(),stu2.get_height()), stu2));
- stu_map.insert(make_pair(key(stu1.get_eyesight(),stu1.get_height()), stu1));
-
map
::iterator iter; - for(iter = stu_map.begin(); iter != stu_map.end();++iter)
- {
- coutfirst.eyesight "\t"first.height "\t" second.get_id()"\t" second.get_name()
- }
- return 0;
- }

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

通過PHP網頁界面添加MySQL用戶可以使用MySQLi擴展。步驟如下:1.連接MySQL數據庫,使用MySQLi擴展。 2.創建用戶,使用CREATEUSER語句,並使用PASSWORD()函數加密密碼。 3.防止SQL注入,使用mysqli_real_escape_string()函數處理用戶輸入。 4.為新用戶分配權限,使用GRANT語句。

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollationsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。