入门计划->学用stl std::map<std::string, int>

博客介绍了C++这一流行的面向对象编程语言及其广阔前景,还提及C++标准模板库stl的强大功能。重点展示了通过名称(string)关键字匹配id(int)的程序代码,该程序在WIN2K+VC6和RedHat Linux 7.2 + gcc(g++)2.96环境测试通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++:一种比较流行的强大功能面向对象编程语言,应用和前景都很广阔。

stl:C++标准模板库,功能极其强大,将其完全掌握后C++编程会相当容易。

map:一种关系式容器,可以根据关键字匹配多种数据。

string:"~!@#$%^&*()_+",这样的东东就是。

以下程序代码将简单演示如何通过名称(string)关键字匹配id(int),由于程式(程序,受网上资料的影响,偶也开始想把程序说成程式,废话)太简单,没有写注释,对于想入门的朋友将代码看明白可以自己举一反三就算学会一招了:)高手请跳过。

程序在以下环境顺利测试通过:

WIN2K+VC6

RedHat Linux 7.2 + gcc(g++)2.96

--源程序--

// strmap1.cpp
//

#pragma warning(disable:4786)
//...
#include <map> 
#include <string> 
//...
#include <iostream>
using namespace std;

class strmap1
{
    typedef std::map<std::string, int> type_map;
    typedef type_map::iterator type_iter;

    type_map    mm;
    type_iter   it;
    int         id;

public:
    strmap1() : it(NULL), id(0)
    {
        //init
        id = 0;
        mm["i"] = ++id;
        mm["you"] = ++id;
        mm["he"] = ++id;
    }

    int find(const char* s)
    {
        cout << "find " << s << endl;
        int ret = 0;
        it = mm.find(s);
        if (mm.end() != it) {
            ret = it->second;
            cout << s << "'s id is " << ret << endl;
        } else {
            cout << "can't find " << s << "'s id" << endl;
        }
        return ret;
    }

    int insert(const char* s)
    {
        cout << "insert " << s << endl;
        int ret = ++id;
        mm.insert(type_map::value_type(s, ret));
        //mm[s] = ret;//ok
        return ret;
    }

    void remove(const char* s)
    {
        cout << "remove " << s << endl;
        mm.erase(s);
    }
};

int main(int argc, char* argv[])
{
    cout << "(strmap1)string map 1(simple use std::map<string, int>)" << endl;
    strmap1 o;

    cout << endl;
    o.find("i");
    
    cout << endl;
    o.find("she");
    
    cout << endl;
    o.find("you");
    o.find("he");

    cout << endl;
    o.insert("she");
    o.remove("you");
    o.remove("he");
    
    cout << endl;
    o.find("you");
    o.find("he");
    o.find("she");
    
    cout << endl;
    cout << "haha~~~now only i and she" << endl;
    return 0;
} 

--输出结果--
(strmap1)string map 1(simple use std::map<string, int>)
find i
i's id is 1
find she
can't find she's id
find you
you's id is 2
find he
he's id is 3
insert she
remove you
remove he
find you
can't find you's id
find he
can't find he's id
find she
she's id is 4
haha~~~now only i and she
# ChildEBP RetAddr Args to Child 00 (Inline) -------- -------- -------- -------- dll_repair!ATL::ChTraitsCRT<wchar_t>::StringCompare+0x5 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 564] 01 (Inline) -------- -------- -------- -------- dll_repair!ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::Compare+0x9 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 1446] 02 (Inline) -------- -------- -------- -------- dll_repair!ATL::operator<+0xe [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 2555] 03 1913f71c 61c5a6a6 0a082580 0b1b0718 6aa97f07 dll_repair!std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >::operator()+0x11 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xstddef @ 141] 04 1913f764 61c5ca28 1913f794 21724f08 0b1b0718 dll_repair!std::_Tree<std::_Tmap_traits<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > >,0> >::_Insert_hint<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > &,std::_Tree_node<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > >,void *> *>+0xf6 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree @ 1686] 05 (Inline) -------- -------- -------- -------- dll_repair!std::_Tree<std::_Tmap_traits<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > >,0> >::emplace_hint+0x28 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree @ 1163] 06 (Inline) -------- -------- -------- -------- dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::_Try_emplace+0x63 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 232] 07 (Inline) -------- -------- -------- -------- dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::try_emplace+0x63 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 248] 08 1913f78c 61c5d871 1913f8b8 1913f8a8 6aa970af dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::operator[]+0x68 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 363]
07-04
(gdb) bt #0 0x005cd5ff in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) () from /usr/lib/libstdc++.so.6 #1 0x080e3b15 in std::_Rb_tree_iterator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> >::operator-- (this=0xbfbacdc4) at /usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:199 #2 0x080e3873 in std::_Rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement>, std::_Select1st<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> > >::_M_insert_unique (this=0x89b53a0, __v=...) at /usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:1179 #3 0x080e31df in std::_Rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement>, std::_Select1st<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> > >::_M_insert_unique_ (this=0x89b53a0, __position=..., __v=...) at /usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_tree.h:1217 #4 0x080e2ee7 in std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, STDTM::AlarmElement, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, STDTM::AlarmElement> > >::insert (this=0x89b53a0, __position=..., __x=...) at /usr/lib/gcc/i686-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_map.h:540
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值