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 $@ $<
%.o:%.cpp
$(CXX) $(CFLAGS) -c -o $@ $<
$(EXEC): $(DEPS) $(OBJS)
$(CXX) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
#ifdef STRIP
# $(STRIP) $@
#endif
all: $(EXEC)
clean:
rm -f *.o $(EXEC)
posted on 2017-05-13 18:42 fanbird2008 阅读(113) 评论(0) 编辑 收藏 举报