фразу слово все слова
 On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение Отправлено: 20.11.15 10:03. Заголовок: Compare two string numeric value

I have two string variable that handle big values like
string a = "24637384636374849";
string b = "67484624274849494";
I need to compare this string like a < b , b < a is there an essay way to do this or i need to write separate function for this . I googled it but i couldn't find any useful info Спасибо: 0 Ответов - 2 [только новые] Отправлено: 20.11.15 10:53. Заголовок: You can compare them..

You can compare them in the lexicographical order.

To define the corresponding logical operator introduce a wrapper around the strings.
For example
`  #include <iostream>  #include <iomanip>  #include <string>    struct BigNumber  {      BigNumber( const std::string &value ) : value( value ) {}      std::string value;  };    bool operator <( const BigNumber &a, const BigNumber &b )  {      std::string::size_type pos1 = a.value .find_first_not_of( '0' );      std::string::size_type pos2 = b.value.find_first_not_of( '0' );            return a.value.compare( pos1, a.value.size() - pos1,              b.value, pos2, b.value.size() - pos2 ) < 0;  }    int main()  {      std::string a = "24637384636374849";      std::string b = "67484624274849494";            std::cout << "a < b is " << std::boolalpha                 << ( BigNumber( a ) < BigNumber( b ) )                << std::endl;                      std::cout << "\"001\" < \"2\" is " << std::boolalpha                 << ( BigNumber( "001" ) < BigNumber( "2" ) )                << std::endl;  }`

The program output is
`  a < b is true  "001" < "2" is true`

Of course you need to provide that the strings contain only digits.

Only you should also append the operator with the code that will process situations when one of the strings or both are empty or contain only zeroe(s).

That is what is the result of comparing a string that contains only zeroe(s) and an empty string?

For example something like the following

`  bool operator <( const BigNumber &a, const BigNumber &b )  {      std::string::size_type pos1 = a.value .find_first_not_of( '0' );      std::string::size_type pos2 = b.value.find_first_not_of( '0' );            if ( pos1 == std::string::npos && pos2 == std::string::npos )       {          return false;      }      else if ( pos1 == std::string::npos )      {          return true;      }      else if ( pos2 == std::string::npos )      {          return false;      }      else      {          return a.value.compare( pos1, a.value.size() - pos1,                  b.value, pos2, b.value.size() - pos2 ) < 0;      }                }`

Take into account that the code I showed does not considers the situation when one string is longer than other. Of course before applying member function compare you have to compare the resulting lengths of the compared substring. If one string is larger than other then the corresponding number is greater than the number in other string.

For example
`      //...      else      {          return ( a.value.size() - pos1 < b.value.size() - pos2 ) ||                 ( !( b.value.size() - pos2 < a.value.size() - pos1 ) &&                 a.value.compare( pos1, a.value.size() - pos1,                  b.value, pos2, b.value.size() - pos2 ) < 0 );      }              `

 Спасибо: 1 Отправлено: 20.11.15 12:26. Заголовок: Wow thanks that all ..

Wow thanks that all i want Спасибо: 0 Ответ: 1 2 3 4 5 6 7 8 9

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться
 Тему читают: - участник сейчас на форуме - участник вне форума Все даты в формате GMT  3 час. Хитов сегодня: 2 Права: смайлы да, картинки да, шрифты да, голосования нет аватары да, автозамена ссылок вкл, премодерация откл, правка нет
{"ft_lang_label":"__label__en","ft_lang_prob":0.5673205,"math_prob":0.94068706,"size":2366,"snap":"2022-40-2023-06","text_gpt3_token_len":647,"char_repetition_ratio":0.17696866,"word_repetition_ratio":0.22997417,"special_character_ratio":0.34404057,"punctuation_ratio":0.2704918,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9854243,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84],"im_url_duplicate_count":[null,6,null,null,null,null,null,null,null,null,null,null,null,null,null,3,null,null,null,null,null,null,null,null,null,null,null,6,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-04T22:43:32Z\",\"WARC-Record-ID\":\"<urn:uuid:d9e7e705-dd00-4c11-90ae-fceb877d0947>\",\"Content-Length\":\"39660\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7d9ec6ae-9bf5-4f43-9df2-e33c1b6f7a57>\",\"WARC-Concurrent-To\":\"<urn:uuid:707b128b-9ecb-4d18-b174-b71273de4c6a>\",\"WARC-IP-Address\":\"91.194.2.65\",\"WARC-Target-URI\":\"https://cpp.forum24.ru/?1-1-60-00000073-000-0-0\",\"WARC-Payload-Digest\":\"sha1:SD36F2WALXWOLT6BCNFM3EVXXBTMP65G\",\"WARC-Block-Digest\":\"sha1:C2PCTPUU7OOGB3QSFZCGH5VXMA4QCIJS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337529.69_warc_CC-MAIN-20221004215917-20221005005917-00711.warc.gz\"}"}