C ++中是否有类似于Java的CompareTo方法,您可以在其中使用> < =对数据类型的操作 [英] Is there a CompareTo method in C++ similar to Java where you can use > < = operations on a data type

查看:133
本文介绍了C ++中是否有类似于Java的CompareTo方法,您可以在其中使用> < =对数据类型的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



背景:
Im创建一个修改后的字符串类,其中包含一个字符串和一个arraylist。我希望能够以传统方式比较该字符串,如果该字符串的字母表中的下限小于该值,则该上限小于该值。比我只希望将数组列表链接到文件,以存储在文本文件中对单词进行索引的页面。无论如何,具体细节并不重要,因为我已经编写了课程。我只需要创建compareTo方法即可在我的cpp文件的主目录中使用,或者可以由其他数据类型(例如各种树)使用。



我会用Java编写代码,因为我知道怎么做,也许有人可以用C ++语法来帮助我(不幸的是,我需要为此项目用C ++编写代码,我是新手



我将缩短代码以给出即时消息的大致轮廓,而不是像我在Java中那样编写compareTo方法

 类名ModifiedString 
具有变量:word,arraylist页面列表
方法:
getWord(返回与该类关联的单词,即其字符串)
appendPageList(将页码添加到数组列表中,在此问题中这无关紧要)

她将如何在Java中做

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

然后,当< ,>或==在ModifiedWord上使用,将导致操作无效。

解决方案

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

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

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

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

  page_map [current_word] .push_back(current_page); 

请注意,我使用了 std :: map 以上,期望您可能希望得到有序的结果(例如,能够快速找到从 age ale (按字母顺序)。如果您不关心订购,则可以改用 std :: unordered_map



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

  #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> >指数;

名称空间std {
ostream& operator<<(ostream& os,index :: value_type const& i){
os<< i。第一<< :\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;

而(>>临时)
i [temp] .push_back(num);
}

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

而(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 ));
返回0;
}

如果您查看第一个 typedef (属于索引),可以将其从 map 更改为 unordered_map 如果要测试哈希表与红黑树。请注意,这对单词的解释相当宽松-基本上是任何非空白字符序列,因此,例如,它将 example,视为单词(并且它将与示例)分开。



请注意,这使用了 infix_iterator 我发布了其他地方


I know that in java there is a compareTo method that you can write in a class that will compare two variables and return a value -1, 1, or 0 signifing greater than, less than, and equal to operations. Is there a way to do this in C++?

Background: Im creating a modified string class in which it takes a string and an arraylist. I want to be able to compare the string in a traditional fashion where if its lower in the alphabet it will be less than, than higher it would be greater than. Than i just want the array list to be linked to the files to store pages in which the word was indexed on in a text file. Anyways the specifics do not matter since i already have the class written. I just need to create compareTo method that would be able to be used in the main of my cpp file or by other data type like various trees for instance.

Ill write the code in java as i know how and maybe someone can help me with C++ Syntax (im required to write in c++ for this project unfortunatly, and i am new to C++)

I will shorten the code to give the rough outline of what im doing than write the compareTo method as i know how in java

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)

Hers how i would do it in java

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

Then when < , > , or == is used on a ModifiedWord than the operations would be valid.

解决方案

std::string already includes a working overload of operator<, so you can just compare strings directly. Java uses compareTo primarily because the built-in comparison operator produces results that aren't generally useful for strings. Being a lower-level language, Java doesn't support user-defined operator overloads, so it uses compareTo as a band-aid to cover for the inadequacy of the language.

From your description, however, you don't need to deal with any of that directly at all. At least as you've described the problem, you really want is something like:

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

You'll then read words in from your text file, and insert the page number where each occurs into the page map:

page_map[current_word].push_back(current_page);

Note that I've used std::map above, on the expectation that you may want ordered results (e.g., be able to quickly find all words from age to ale in alphabetical order). If you don't care about ordering, you may want to use std::unordered_map instead.

Edit: here's a simple text cross-reference program that reads a text file (from standard input) and writes out a cross-reference by line number (i.e., each "word", and the numbers of the lines on which that word appeared).

#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;
}

If you look at the first typedef (of index), you can change it from map to unordered_map if you want to test a hash table vs. a red-black tree. Note that this interprets "word" pretty loosely -- basically any sequence of non-whitespace characters, so for example, it'll treat example, as a "word" (and it'll be separate from example).

Note that this uses the infix_iterator I've posted elsewhere.

这篇关于C ++中是否有类似于Java的CompareTo方法,您可以在其中使用&gt; &lt; =对数据类型的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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