C++ sort array of strings

后端 未结 6 1890
渐次进展
渐次进展 2021-01-01 15:28

I am trying to sort an array of strings, but it\'s not sorting anything.... what am I doing wrong?

string namesS[MAX_NAMES];

int compare (const void * a, co         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-01 15:57

    You can use boost::sort, like this:

    #include 
    #include 
    
    std::vector stringarray;
    boost::sort(stringarray);
    

    If you want use find use boost::find, like this:

    std::string findme;
    auto offset = boost::find(stringarray, findme) - stringarray.begin()
    

    See 2 useful functions (m_stringarray should be member of ClassA):

    const size_t ClassA::GetIdByName(std::string name) const
    {
        return (boost::find(this->m_stringarray, name) - this->m_stringarray.begin());
    }
    
    const std::string ClassA::GetNameById(size_t id) const
    {
        return this->m_stringarray[id];
    }
    

提交回复
热议问题