Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向向量插入字符串_C++_String_Vector_Insert - Fatal编程技术网

C++ 向向量插入字符串

C++ 向向量插入字符串,c++,string,vector,insert,C++,String,Vector,Insert,我得到一个编译错误。我试图将字符串添加到向量中,并使它们保持“排序顺序” XYZ是我的班级。阿登特里 class XYZ { public: portListFile(string sTmp); void addPortEntry(string sPortName, string sDirection); private: string sPortListFileName; vector <string>

我得到一个编译错误。我试图将字符串添加到向量中,并使它们保持“排序顺序”

XYZ是我的班级。阿登特里

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
    }
    ...
}
XYZ类
{
公众:
portListFile(字符串sTmp);
void addPortEntry(字符串sPortName、字符串sddirection);
私人:
字符串列表文件名;
矢量v_输入_端口;
...
};
void XYZ::addP(字符串sP、字符串sDir)
{
如果(sDir==“in”)
{
v_input_ports.insert(sP);//第42行
}
...
}
错误:

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> > >]
XYZ.cpp:在成员函数“void XYZ::addP(std::string,std::string)”中:
XYZ.cpp:42:错误:调用“std::vector::insert(const char[10])时没有匹配的函数
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../../../../../../include/c++/4.1.2/bits/vector.tcc:93:注意:候选项为:typename std::vector::vector std::iterator std::vector::insert(u gnu_u_u_cxx:::u normal_uiterator,const&)[带Tp=std::basic字符串,u Alloc=std::分配器]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../../../../../../include/c++/4.1.2/bits/stl_vector.h:657:注意:void std::vector::insert(u gnu_cxx:u normal_u迭代器,size_t,constp&)[带Tp=std::basic_string,Alloc=std::分配器]

也许你的意思是
向后推
而不是
插入

应该给一个迭代器在某个位置插入。您需要改为使用(与
insert
相同,使用
end()
作为参数)

编辑

你在评论中提到:


我不想使用push_back,因为我想对字符串进行排序。 这就是我使用向量的原因之一


我不理解那句话的逻辑。如果你想要一个分类的容器,你应该使用或。如果需要重复值,请使用“多-”版本。

一般来说,您可以将元素附加到向量 (
push_back
),也可以将其插入指定位置(
insert
)。到 在指定位置插入对象,必须指定位置;
std::vector::insert
接受两个参数,第一个是迭代器 指定要插入的值的位置和第二个值

你说你想保持内容的有序。照常 习惯用法是使用
std::lower_bound
查找位置,例如:

void
XYZ::addP( std::string const& sP, std::string const& sDir )
{
    if ( sDir == "in" ) {
        std::vector<std::string>::iterator pos
            = std::lower_bound( v_input_ports.begin(),
                                v_input_ports.end(),
                                sP );
        if ( pos != v_input_ports.end() && *pos == sDir ) {
            //  Object already present...
            *pos = sP;  //  But maybe an error is more appropriate
        } else {
            v_input_ports.insert( pos, sP );
        }
    }
}
void
XYZ::addP(标准::字符串常量和sP,标准::字符串常量和sDir)
{
如果(sDir==“in”){
标准::向量::迭代器位置
=std::下限(v_输入_端口.begin(),
v_输入_端口.end(),
sP);
if(pos!=v_输入_端口.end()&&*pos==sDir){
//对象已存在。。。
*pos=sP;//但可能一个错误更合适
}否则{
v_输入_端口。插入(位置、sP);
}
}
}
不过,有两个简短的评论:

  • 您可能应该通过常量引用传递
    std::string
    , 而不是价值。无论出于何种原因,这几乎是最好的选择 普遍的惯例,如果你不遵守它,人们会好奇 为什么

  • 任何时候,当您关心标准中的顺序时,您都需要 定义排序关系。默认值为
    std::less
    ,由
    默认值不
    当您(可能)没有使用过的函数出现编译错误时,请查看函数的文档。您将确切地了解如何使用它们,并可能在同一地点找到更好的东西。在这种情况下,
    push_back
    会列在旁边,因为它也是
    vector
    的一部分,如果您碰巧看到名称,它会吸引您的注意力。我不想使用push_back,因为我希望对字符串进行排序。这就是我使用向量的原因之一。@KingkongJnr一个向量与另一个向量的关系如何?为什么不使用地图>抱歉。我应该清楚地提到这两种说法之间的区别。我不想使用push_back,因为我想对字符串进行排序。此外,这也是我使用向量的原因之一(暗示我愿意接受建议,以防向量无法做到这一点)。我将探索如何使用std::set。我不想使用地图,因为它不是一对数据,只是一个。单个元素都是像“Toyota”、“Honda”之类的刺。如果是像“Toyota,100”、“Honda,300”之类的东西,那么地图会更有意义(我猜)。非常感谢您的及时回复。