Sort std::vector<std::string *> overloading <

问题描述 投票:0回答:1
  • 我想使用 std::sort() 关于 std::vector<std::string *>.
  • 为此,我想过载 <.
  • 我想用这个运算符来比较字符串中的第一个字符。
  • 我想 std::sort() 函数没有调用我的运算符,因为向量实际上有 std::string * 代替 std::string. 但我不知道如何指定这个参数。
  • 我想在不使用第三个参数的情况下实现这个目标。std::sort()
#include <iostream>
#include <vector>
#include <algorithm>

bool operator<(std::string& str_1, std::string& str_2){
    std::cout<<str_1[0] <<" < " << str_2[0] << std::endl;
    return str_1[0] < str_2[0];
}
int main(int argc, const char * argv[]) {
    std::string str1 = "abc";
    std::string str2 = "def";
    std::string str3 = "ghi";
    std::string str4 = "bcd";
    std::string str5 = "fgh";

    //test the operator first
    std::cout << (str1<str2) << std::endl;

    std::vector<std::string *> str_vector;
    str_vector.push_back(&str1);
    str_vector.push_back(&str2);
    str_vector.push_back(&str3);
    str_vector.push_back(&str4);
    str_vector.push_back(&str5);

    std::sort(str_vector.begin(), str_vector.end());

    for (std::vector<std::string *>::iterator it=str_vector.begin(); it!=str_vector.end(); ++it)
        std::cout << ' ' << **it << std::endl;

    return 0;
}

操作符似乎被很好地实现了,但从未在 std::sort()这是控制台的输出。

a < d
1
 fgh
 bcd
 ghi
 def
 abc
Program ended with exit code: 0

THANKS!

c++ sorting c++11 operator-overloading stdvector
1个回答
1
投票

你已经超载了 operator< 对于 string::stringstr_vector 包含 std::string *.

因为你不能改变基元类型(如指针)运算符的行为,所以你需要提供一个自定义的比较器作为最后一个参数,以使 std::sort. 例如作为lambda。

std::sort(str_vector.begin(), str_vector.end(),
[](const std::string *str_1, const std::string *str_2){
    std::cout << (*str_1)[0] <<" < " << (*str_2)[0] << std::endl;
    return (*str_1)[0] < (*str_2)[0];
});

0
投票

你有一个指向对象类型的向量 std::string

std::vector<std::string *> str_vector;

所以,如果你将调用标准算法 std::sort 而不指定比较函数,那么算法将尝试比较指针本身而不比较指向的对象来对向量进行排序。

但是如果你将一个对象的向量声明为 std::string 而不是指针,但你的过载 operator < 将无法找到,因为函数的定义是 std::sort 是在命名空间 std 而在这个命名空间中,可以找到标准的重载的 operator <.

你可以使用你自己的重载的 operator < 如果你将使用自己的比较函数,例如lambda表达式。

下面是一个演示程序。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

bool operator <( const std::string &s1, const std::string &s2 )
{
    std::cout << "Bingo\n";

    return s1[0] < s2[0];
}

int main() 
{
    std::string s[] = { "abc", "def", "ghi", "bcd", "fgh" };

    std::vector<std::string *> v1;
    v1.reserve( sizeof( s ) / sizeof( *s ) );

    std::transform( std::begin( s ), std::end( s ), std::back_inserter( v1 ),
                    []( auto &s ) { return &s; } );

    std::sort( std::begin( v1 ), std::end( v1 ), 
               []( const auto &p1, const auto p2 )
               {
                    return *p1 < *p2;
               } );                 

    for ( const auto &p : v1 )
    {
        std::cout << *p << ' ';
    }
    std::cout << '\n';

    return 0;
}

程序的输出是

Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
Bingo
abc bcd def fgh ghi 

正如你可以看到由于测试输出的字符串 "Bingo" 你的 operator < 被称为。

© www.soinside.com 2019 - 2024. All rights reserved.