如何在c ++中插入和排序具有唯一项的对象向量? [英] how to insert and sort a vector of objects with unique entries in c++?

查看:143
本文介绍了如何在c ++中插入和排序具有唯一项的对象向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,仅在不存在将对象排序的情况下才将对象插入到对象向量中.因此,为此,我包括了算法头并使用查找和排序功能,我试图执行相同的功能,但这给了我一个我无法理解和解决的错误.

I am working on a program to insert objects into vector of objects only if it does not exist already keeping them sorted. so for that i have included algorithm header and using find and sort functions i am trying to execute the same but it is giving me an error I am not able to understand and solve.

这是插入函数,它返回单词类的对象,而数据是单词的向量.

This is the function for insertion where it is returning a an object of class word and data is a vector of words.

    Word *WordVector::insert(const string text){
Word newWord(text);
if(data.size()==0)
{
    data.push_back(newWord);
}

else{
    if(find(data.begin(),data.end(),newWord)!=data.end()){
        newWord.increaseCount();
    }
    else data.push_back(newWord);
    std::sort(data.begin(), data.end());
}

return &newWord;

}

在第7行的此方法中,它在算法文件中给我这个错误二进制表达式('Word'和'const Word')的无效操作数"

and it gives me this error "Invalid operands to binary expression ('Word' and 'const Word')" in algorithm file at this method at line 7

    template <class _InputIterator, class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
_InputIterator
find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
{
    for (; __first != __last; ++__first)
        if (*__first == __value_)
            break;
    return __first;
}

推荐答案

我会使用 std :: lower_bound .它会在已排序的容器中返回正确的 insert 位置,以保持元素已排序.

I would use std::lower_bound for this. It returns the correct insert position in a sorted container to keep the elements sorted.

bool insert_sorted(std::vector<int>& v, int n)
{
    // find the correct sorted insert position
    auto insert_itr = std::lower_bound(std::begin(v), std::end(v), n);

    // only insert if not a duplicate
    // test for end() first to make duplicate test safe
    if(insert_itr == std::end(v) || *insert_itr != n)
    {
        v.insert(insert_itr, n);
        return true;
    }

    return false;
}

int main()
{
    std::vector<int> ints;

    for(auto i = 0; i < 10; ++i)
        insert_sorted(ints, hol::random_number(0, 10));

    for(auto i: ints)
        std::cout << i << '\n';
}

示例输出:

0
3
4
7
9
10

要使其在用户定义的类型上起作用,您将需要定义某些功能,这些功能可使class进行排序 并测试(in)是否相等

To get this to work on a user defined type you will need to define certain functions that enable the class to be sorted and tested for (in)equality.

例如:

class Word
{
public:
    Word(std::string const& s): s(s) {}

    // to enable sorting
    bool operator<(Word const& w) const { return s < w.s; }

    // enable equality matching
    bool operator==(Word const& w) const { return s == w.s; }
    bool operator!=(Word const& w) const { return s != w.s; }

    // enable printing out
    friend std::ostream& operator<<(std::ostream& os, Word const& w)
    {
        return os << w.s;
    }

private:
    std::string s;
};

bool insert_sorted(std::vector<Word>& v, Word const& w)
{
    // find the correct sorted insert position
    auto insert_itr = std::lower_bound(std::begin(v), std::end(v), w);

    // only insert if not a duplicate
    // test for end() first to make duplicate test safe
    if(insert_itr == std::end(v) || *insert_itr != w)
    {
        v.insert(insert_itr, w);
        return true;
    }

    return false;
}

这篇关于如何在c ++中插入和排序具有唯一项的对象向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆