c++ - C++ 中是否有类似于 Java 的 CompareTo 方法,您可以在其中对数据类型使用 > < = 操作

标签 c++ class compare string-comparison

我知道在 Java 中有一个 compareTo 方法,您可以在类中编写该方法来比较两个变量并返回值 -1、1 或 0,表示大于、小于和等于操作。有没有办法在 C++ 中执行此操作?

背景: 我正在创建一个修改后的字符串类,其中它采用一个字符串和一个数组列表。我希望能够以传统方式比较字符串,如果它在字母表中较低,它将小于,高于它会大于。比我只想将数组列表链接到文件以存储文本文件中索引该词的页面。无论如何,细节并不重要,因为我已经写好了类(class)。我只需要创建 compareTo 方法,该方法可以在我的 cpp 文件的主体中使用,也可以由其他数据类型(例如各种树)使用。

我会用我知道的方式用 Java 编写代码,也许有人可以帮助我使用 C++ 语法(不幸的是,我需要用 C++ 为这个项目编写代码,而我是 C++ 的新手)

我将缩短代码以给出我正在做的事情的粗略概述,而不是像我在 java 中知道的那样编写 compareTo 方法

class name ModifiedString
Has variables: word , arraylist pagelist
Methods: 
getWord (returns the word associated with the class, i.e its string)
appendPageList (adds page numbers to the array list, this doesnt matter in this question)

她是我如何用 java 做的

int compareTo(ModifiedString a){
  if(this.getWord() > a.getWord())
      return 1;
  else if (this.word() < a.getWord())
      return -1;
  else return 0;
}

然后,当 < 、 > 或 == 用于 ModifiedWord 时,操作将有效。

最佳答案

std::string已经包含了 operator< 的工作重载, 所以你可以直接比较字符串。 Java 使用 compareTo主要是因为内置的比较运算符产生的结果通常对字符串没有用。作为一种低级语言,Java 不支持用户定义的运算符重载,因此它使用 compareTo作为弥补语言不足的创可贴。

但是,根据您的描述,您根本不需要直接处理任何。至少正如您所描述的问题,您真正想要的是:

std::map<std::string, std::vector<int> > page_map;

然后您将从文本文件中读取单词,并将每个单词出现的页码插入到页面映射中:

page_map[current_word].push_back(current_page);

请注意,我使用了 std::map以上,基于您可能想要有序结果的期望(例如,能够按字母顺序快速找到从 ageale 的所有单词)。如果您不关心订购,您可能想使用 std::unordered_map相反。

编辑:这是一个简单的文本交叉引用程序,它读取文本文件(从标准输入)并按行号写出交叉引用(即,每个“单词”,以及该单词所在的行数单词出现了)。

#include <map>
#include <unordered_map>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include "infix_iterator.h"

typedef std::map<std::string, std::vector<unsigned> > index;

namespace std {
ostream &operator<<(ostream &os, index::value_type const &i) { 
    os << i.first << ":\t";
    std::copy(i.second.begin(), i.second.end(),
        infix_ostream_iterator<unsigned>(os, ", "));
    return os;
}
}

void add_words(std::string const &line, size_t num, index &i) { 
    std::istringstream is(line);
    std::string temp;

    while (is >> temp)
        i[temp].push_back(num);
}

int main() { 
    index i;
    std::string line;
    size_t line_number = 0;

    while (std::getline(std::cin, line))
        add_words(line, ++line_number, i);

    std::copy(i.begin(), i.end(), 
        std::ostream_iterator<index::value_type>(std::cout, "\n"));
    return 0;
}

如果你看第一个typedef (属于 index ),您可以将其更改为 mapunordered_map如果你想测试哈希表和红黑树。请注意,这对“单词”的解释非常松散——基本上是任何非空白字符序列,例如,它将处理 example,作为一个“词”(它将与 example 分开)。

请注意,这使用了 infix_iterator我已经发布了 elsewhere .

关于c++ - C++ 中是否有类似于 Java 的 CompareTo 方法,您可以在其中对数据类型使用 > < = 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005392/

相关文章:

c++ - Qt C++ LNK2019 : unresolved external symbol using QNetworkAccessManager

c++ - 我能以某种方式使用继承吗

php - mysqli_query() 期望参数 1 为 mysqli,对象给定

c++ - 类——获取函数——返回多个值

c - 查找 argv[] 中的重复字符

c# - 按字典顺序比较两个 char 数组

Delphi:TStringList.Contains?

c++ - QPainter : adding padding

c++ - 将原始内存写入文件

c++ - C++ 中的私有(private)函数与静态函数