std::map Intro

#include <queue>
#include <map>
#include <iostream>
#include <string.h>

class TestU {
public:
TestU(char *);
~TestU();
char *getData();
private:
char *data;
};

TestU::TestU(char *d)
{
if(d) {
int len = strlen(d);
data = new char[len + 1];
strcpy(data, d);
}
}

TestU::~TestU()
{
if(data) {
delete [] data;
}
}

char *TestU::getData()
{
return data;
}

int main(int argc, char **argv)
{
std::queue<TestU>qu;
std::queue<TestU *>q;
std::map<int, TestU *>m;

for(int i = 0; i < 10; i++) {
char str[1024];
sprintf(str, "I am %d", i);
TestU *a = new TestU(str);
TestU *b = new TestU(str);
TestU *c = new TestU(str);
q.push(a);
qu.push(*b);
m[i] = c;
}

while(! q.empty()) {
TestU *a = q.front();
printf("a = [%s]\n", a->getData());
q.pop();
printf("a000 = [%s]\n", a->getData());
delete a;
}

while(! qu.empty()) {
TestU& a = qu.front();
printf("au = [%s]\n", a.getData());
qu.pop();

// printf("au111 = [%s]\n", a.getData());
}

while(! m.empty()) {
TestU *u = m.begin()->second;
printf("m = [%s]\n", u->getData());
m.erase(m.begin());
printf("mm = [%s]\n", u->getData());
delete u;
}

return 0;
}

DEPS=
EXEC=test.x

SRCS=$(wildcard *.c *.cpp)
#SRCS1=$(filter-out dec.c, $(SRCS))
#FNAMES1=$(notdir $(SRCS1))
OBJS=$(patsubst %.c,%.o,$(SRCS))

#CFLAGS += -g -Wall -Wno-unused-result -std=c++11 -std=gnu++11 -fpic -fPIC -D_LARGEFILE64_SOURCE -DUSE_IMPORT_EXPORT
CFLAGS += -g -Wall -Wno-unused-result -std=c++11 -std=gnu++11 -fpic -fPIC -D_LARGEFILE64_SOURCE -DUSE_IMPORT_EXPORT

CFLAGS += -I./include
LDFLAGS += -pthread

%.o: %.c
$(CC) $(CFLAGS) -c -o [email protected] $<

%.o:%.cpp
$(CXX) $(CFLAGS) -c -o [email protected] $<

$(EXEC): $(DEPS) $(OBJS)
$(CXX) $(CFLAGS) -o [email protected] $(OBJS) $(LDFLAGS)

#ifdef STRIP
# $(STRIP) [email protected]
#endif

all: $(EXEC)

clean:
rm -f *.o $(EXEC)

时间: 2024-08-10 01:56:19

std::map Intro的相关文章

std::map使用结构体自定义键值

使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; mp[(Node){x,y}]++; 这样子的话,会出现一堆报错 c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_function.h||In instantiation of 'bool std::less<_Tp>::operator()(

c++ how to make your own class a valid key type for std::map?

In Java, if you want your own class to be a valid key type of the container, you just need to make it implement the interface "Comparable", and then it'll work properly. How about in C++? I was wondering whether C++ has offered some kind of mech

std::map常用方法

map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair("Peter Q.", 5328)); std::map常用方法,布布扣,bubuko.com

STL之std::set、std::map的lower_bound和upper_bound函数使用说明

由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊的函数,lower_bound.upper_bound.equal_range. 原型如下: iterator lower_bound (const value_type& val) const; iterator upper_bound (const value_type& val) con

stl std::map容器排序及使用注意事项 .

01.#include "stdafx.h" 02.#include <map> 03.#include <iostream> 04. 05.int _tmain(int argc, _TCHAR* argv[]) 06.{ 07. /** 08. * map中的每个元素都是一个pair类型 09. * 对于插入其中的元素都会默认按键值升序排列好 10. */ 11. 12. std::map<int, int> m; 13. m.insert(st

RAD C++Builder xe7 std::map xtree BUG

c++Builder 6 下的std::map还能用,有代码提示. 换到xe7,代码提示出来就一个tt.operator [](),代码没法往下写了. 最后把Target Platforms切换到64 bit windows 竟然可以了!!!

C++ std::map::erase用法及其陷阱

1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string,string>::iterator ITER; ITER iter=mapTest.find(key); mapTest.erase(iter); 像上面这种删除单个节点,map的行为不会出现问题,但是当在一个循环里用的时候,往往会被误用. 2.陷阱 eg: for(ITER iter=mapTest

std::map插入已存在的key时,key对应的内容不会被更新

std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the

boost::unordered_map 和 std::map 的效率 与 内存比较

例子链接:http://blog.csdn.net/gamecreating/article/details/7698719 结论: unordered_map 查找效率快五倍,插入更快,节省一定内存.如果没有必要排序的话,尽量使用 hash_map(unordered_map 就是 boost 里面的 hash_map 实现). boost::unordered_map 和 std::map 的效率 与 内存比较