java可以用 lt =判断int吗,C ++中是否有类似于Java的CompareTo方法,您可以在其中使用> < =对数据类型的操作...

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

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<:string std::vector> > 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

#include

#include

#include

#include

#include

#include

#include "infix_iterator.h"

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

namespace std {

ostream &operator<

os << i.first << ":\t";

std::copy(i.second.begin(), i.second.end(),

infix_ostream_iterator(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<: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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值