标准库容器中的const元素类型 [英] const element type in standard library containers

查看:62
本文介绍了标准库容器中的const元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划1:

---------------

#include< cstdlib>

#include< iostream>

#include< vector>

using namespace std;


int main()

{

vector< const intc2;


返回EXIT_SUCCESS;

}


这个程序给出了矢量实例化的编译错误。


现在考虑以下程序2:

- -------------------------------------------------- ---

#include< cstdlib>

#include< iostream>

#include< set>

#include< utility>


using namespace std;


int main()

{

typedef对< const int,const intp_type;


multiset< p_typec1;


c1.insert(make_pair (0,2));


返回EXIT_SUCCESS;

}


但是第二个程序也有' 利弊t int''作为元素的一部分

类型;但它编译得很好。


为什么第一个程序没有编译,为什么第二个程序

编译好了?

请澄清。


谢谢

V.Subramanian

Program 1:
---------------
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<const intc2;

return EXIT_SUCCESS;
}

This program gives compilation error for the vector instantiation.

Now consider the following program 2:
-------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <set>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

multiset<p_typec1;

c1.insert(make_pair(0, 2));

return EXIT_SUCCESS;
}

But this second program also has ''const int'' as part of the element
type; but it compiles fine.

Why doesn''t the first program compile and why does the second program
compile fine ?

Kindly clarify.

Thanks
V.Subramanian

推荐答案

考虑程序z.cpp:


#include< cstdlib>

#include< iostream>

#include< vector>

#include< deque>

#include< list>

#include< map>

#include< utility>

使用命名空间std;


int main()

{

typedef pair< const int,const intp_type;


vector< p_typev;

//v.insert( v.begin(),make_pair(0,2));


list< p_typel;

l.insert(l.begin(),make_pair( 0,2));


deque< p_typed;

//d.insert(d.begin(),make_pair(0,2));


map< const int,const int s;

s.insert(make_pair(0,2));


返回EXIT_SUCCESS;

}


我正在使用

g ++ -std = c ++ 98 -pedantic -Wall -Wextra z.cpp


这里如果我删除了两条注释(矢量插入和deque各一个

插入),我得到编译错误。但是对于列表,类似的插入没有

编译错误。为什么编译错误

出现,仅适用于矢量和双端队列,而不适用于列表和地图?


请澄清。


谢谢

V.Subramanian
Consider the program z.cpp:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

vector<p_typev;
//v.insert(v.begin(), make_pair(0, 2));

list<p_typel;
l.insert(l.begin(), make_pair(0, 2));

deque<p_typed;
//d.insert(d.begin(), make_pair(0, 2));

map<const int, const ints;
s.insert(make_pair(0, 2));

return EXIT_SUCCESS;
}

I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Here if I remove the two comments(one each on vector insert and deque
insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?

Kindly clarify.

Thanks
V.Subramanian


su ************** @ yahoo.com 写道:

考虑程序z.cpp:


#include< cstdlib>

#include< iostream>

#include<向量>

#include< deque>

#include< list>

#include< map>

#include< utility>


使用命名空间std;


int main()

{

typedef pair< const int,const intp_type;


vector< p_typev;

//v.insert(v.begin( ),make_pair(0,2));


list< p_typel;

l.insert(l.begin(),make_pair(0,2) );


deque< p_t yped;

//d.insert(d.begin(),make_pair(0,2));


map< const int,const ints;

s.insert(make_pair(0,2));


返回EXIT_SUCCESS;

}


我正在使用

g ++ -std = c ++ 98 -pedantic -Wall -Wextra z.cpp


如果我删除这两个注释(每个矢量插入一个和

deque insert),我得到编译错误。但是对于列表,类似的插入没有

编译错误。为什么会出现编译错误

,仅适用于vector和deque,而不适用于列表和映射?
Consider the program z.cpp:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>

using namespace std;

int main()
{
typedef pair<const int, const intp_type;

vector<p_typev;
//v.insert(v.begin(), make_pair(0, 2));

list<p_typel;
l.insert(l.begin(), make_pair(0, 2));

deque<p_typed;
//d.insert(d.begin(), make_pair(0, 2));

map<const int, const ints;
s.insert(make_pair(0, 2));

return EXIT_SUCCESS;
}

I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp

Here if I remove the two comments(one each on vector insert and
deque insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?



标准容器需要值类型为转让。当

这对成员是const时,它不是。


使用列表和地图,你只需在
$时就可以逃脱b $ b赋值运算符不被实现使用。

Bo Persson

Standard containers require that the value type is assignable. When
the pair members are const, it is not.

With list and map, you just get away with this violation when the
assignment operator isn''t used by the implementation.
Bo Persson


5月28日,下午1:51, Bo Persson < b ... @ gmb.dkwrote:
On May 28, 1:51 pm, "Bo Persson" <b...@gmb.dkwrote:

subramanian10 ... @ yahoo.com写道:
subramanian10...@yahoo.com wrote:

考虑程序z.cpp:
Consider the program z.cpp:


#include< cstdlib>

#include< iostream>

#include< vector>

#include< deque>

#include< list>

#include< map>

#include< utility>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>


using namespace std;
using namespace std;


int main()

{

typedef pair< const int,const intp_type;
int main()
{
typedef pair<const int, const intp_type;


vector< p_typev;

//v.insert(v.begin(),make_pair(0,2) );
vector<p_typev;
//v.insert(v.begin(), make_pair(0, 2));


list< p_typel;

l.insert(l.begin(),make_pair(0,2));
list<p_typel;
l.insert(l.begin(), make_pair(0, 2));


deque< p_typed;

//d.insert(d.begin(),make_pair(0,2) );
deque<p_typed;
//d.insert(d.begin(), make_pair(0, 2));


map< const int,const ints;

s.insert(make_pair(0,2));
map<const int, const ints;
s.insert(make_pair(0, 2));


返回EXIT_SUCCESS;

}
return EXIT_SUCCESS;
}


我正在使用

g ++ -std = c ++ 98 -pedantic -Wall -Wextra z.cpp
I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp


在这里,如果我删除两个注释(向量插入和

deque插入一个),我得到编译错误。但是对于列表,类似的插入没有

编译错误。为什么会出现编译错误

,仅适用于矢量和双端队列而不适用于列表和地图?
Here if I remove the two comments(one each on vector insert and
deque insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?



标准容器要求值类型是可分配的。当

这对成员是const时,它不是。


使用列表和地图,你只需在
$时就可以逃脱b $ b赋值运算符不被实现使用。


Bo Persson


Standard containers require that the value type is assignable. When
the pair members are const, it is not.

With list and map, you just get away with this violation when the
assignment operator isn''t used by the implementation.

Bo Persson



这是否意味着CopyConstructible和可分配的要求

对于列表,地图不是强制性的?相反,他们是否实施 -

依赖?


请澄清。


谢谢

V.Subramanian

Does this mean that the CopyConstructible and Assignable requirements
are not mandatory for list, map ? Rather, are they implementation-
dependent ?

Kindly clarify.

Thanks
V.Subramanian


这篇关于标准库容器中的const元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆