将字符串插入向量

Kin*_*Jnr 2 c++ string insert vector

我收到编译错误.我正在尝试向向量添加字符串并将其保持为"排序顺序".

XYZ是我的班级.addPortEntry

class XYZ
{
    public:
        portListFile(string sTmp);
        void addPortEntry(string sPortName, string sDirection);
    private:
        string sPortListFileName;
        vector <string> v_input_ports;
    ...
};

void XYZ::addP(string sP, string sDir)
{
    if(sDir == "in")
    {
        v_input_ports.insert(sP);   // Line 42
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

错误:

XYZ.cpp: In member function ‘void XYZ::addP(std::string, std::string)’:
XYZ.cpp:42: error: no matching function for call to ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::insert(const char [10])’
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:93: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:657: note:                 void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
Run Code Online (Sandbox Code Playgroud)

lit*_*adv 7

insert应该给一个迭代器插入某个位置.您需要改为使用push_back(insertend()参数相同).

编辑

你在评论中提到:

我不想使用push_back,因为我希望对字符串进行排序.这是我使用矢量的原因之一

我想念那句话中的逻辑.如果你想要一个已分类的容器,你应该使用std::set或者std::map.如果要重复值,请使用"多"版本.