在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,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

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 lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中