Boost logo

Boost Users :

Subject: Re: [Boost-users] Concatenating int value to string ..??
From: Rahul Mathur (srivmuk_at_[hidden])
Date: 2013-11-02 08:12:02


YES, the string should contain a valid integer. Thanks all ..!!

BTW.." I have a sinerio as -

1. One string as "Apple"
2. Another string or int value as "01" or 1.
3. Final third being a string or int value as "000" or 1.

CASE:A I have to print as -> Apple01001 with the last three digits from
right being incremented when needed e.g Apple01001, Apple01002, Apple01003
and so on..

CASE:B Also, the fourth and fifth digit from right being incremented when
needed, alongwith last three digits from right as - Apple02001, Apple02002,
Apple02003 and so on ..

Thought boost::lexical_cast would have done as referred from link -
http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance.html

Any approach?

On Sat, Nov 2, 2013 at 5:22 PM, Alain Leblanc <aalebl_at_[hidden]> wrote:

> You can't convert Apple01000 to an int. Cast from string to int is
> possible, but the string must contain a valid integer.
>
> A
> On Nov 2, 2013 7:20 AM, "Rahul Mathur" <srivmuk_at_[hidden]> wrote:
>
>> This given link -
>> http://www.boost.org/doc/libs/1_54_0/doc/html/boost_lexical_cast/performance.htmlsays string->int conversion is possible.
>>
>>
>> On Sat, Nov 2, 2013 at 3:43 PM, Daniel Anderson <wonixen_at_[hidden]>wrote:
>>
>>> 2013/11/2 Rahul Mathur <srivmuk_at_[hidden]>
>>>
>>>> All,
>>>>
>>>> I wish to have string (combination of string value and int value) which
>>>> increments until 100. The code is as below -
>>>>
>>>> ---
>>>> #include <iostream>
>>>> #include <cstdlib>
>>>> #include <string>
>>>> #include <boost/lexical_cast.hpp>
>>>>
>>>> using namespace std;
>>>>
>>>> int main() {
>>>> std::string TID = "Apple";
>>>> int TagID = 01, Value = 000 /* 100 IDs*/;
>>>> std::string TagName, TagInitial, TagUId;
>>>> TagName = TID + boost::lexical_cast<std::string>(TagID); //
>>>> Should be as -> Apple01
>>>> TagInitial = TagName + boost::lexical_cast<std::string>(Value);
>>>> // Should be as -> Apple01000
>>>> int i = boost::lexical_cast<int>(TagInitial) + 1; // Should be
>>>> as -> Apple01001
>>>>
>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> I think this is throwing the exception, the cast
>>> seems invalid
>>>
>>>
>>>> TagUId = boost::lexical_cast<std::string>(i);
>>>> std::cout << "Tag ID .." << TagUId << endl;
>>>> }
>>>>
>>>> I get below error message -
>>>>
>>>> --
>>>> terminate called after throwing an instance of
>>>> 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
>>>> >'
>>>> what(): bad lexical cast: source type value could not be interpreted
>>>> as target
>>>> Aborted (core dumped)
>>>> --
>>>>
>>>> Any clue?
>>>>
>>>> ~ Thanks!!
>>>>
>>>> _______________________________________________
>>>> Boost-users mailing list
>>>> Boost-users_at_[hidden]
>>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>>>
>>>
>>>
>>>
>>> --
>>> Daniel
>>>
>>> D'un coté s'étend le bien, de l'autre, le vide de la vie quotidienne.*
>>> (F.Alberoni)*
>>>
>>> _______________________________________________
>>> Boost-users mailing list
>>> Boost-users_at_[hidden]
>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>>
>>
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net