std::map Intro

Posted fanbird2008

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 

以上是关于std::map Intro的主要内容,如果未能解决你的问题,请参考以下文章

std::map 到 std::list 导致 SIGSEGV

对于 std::tr1::unordered_map,是不是有任何等效的 std::algorithm 类似于 std::map::lower_bound?

将空向量放入 std::map()

使用 std::string 作为 std::map 的键

std::map的insert和下标[]操作区别

std::map::size_type 用于其 value_type 是它自己的 size_type 的 std::map