6 Replies - 1693 Views - Last Post: 14 March 2010 - 12:53 AM Rate Topic: -----

#1 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

How to use copy and boost::lexical_cast

Posted 13 March 2010 - 11:16 PM

Hello. I want to use copy and boost::lexical_cast.
I have two vector, vector<string> strvect and vector<int> intvect. I want to copy the content of strvect into intvect using copy function of algorithm and simultaneously catch any error if occurred. I think a way like:
try
{
    for(vector<string> :: iterator it = strvect.begin(); it != strvect.end(); it++)
    {
        int number = boost::lexical_cast<int> (*it);
        intvect.push_back(number);
    }
}
catch( const boost::bad_lexical_cast & )
{
    //unable to convert
}


But in the above method I can not use copy function. Is there any way to do it?

Is This A Good Question/Topic? 0
  • +

Replies To: How to use copy and boost::lexical_cast

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: How to use copy and boost::lexical_cast

Posted 13 March 2010 - 11:45 PM

Why not just use that method? Do you have to use std::copy?
Was This Post Helpful? 0
  • +
  • -

#3 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to use copy and boost::lexical_cast

Posted 14 March 2010 - 12:27 AM

View PostPlasticineGuy, on 13 March 2010 - 10:45 PM, said:

Why not just use that method? Do you have to use std::copy?

It is not necessary, I just want to learn.
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: How to use copy and boost::lexical_cast

Posted 14 March 2010 - 12:30 AM

What does lexical_cast do, specifically?

I doubt it is possible to use std::copy here, but it might be.
Was This Post Helpful? 0
  • +
  • -

#5 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to use copy and boost::lexical_cast

Posted 14 March 2010 - 12:41 AM

Actually lexical_cast do casting. Like string to int or double.
Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: How to use copy and boost::lexical_cast

Posted 14 March 2010 - 12:43 AM

Ah, so it can turn something like "24" into 24?

You're stuck with the for loop here.
Was This Post Helpful? 0
  • +
  • -

#7 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to use copy and boost::lexical_cast

Posted 14 March 2010 - 12:53 AM

Yeah, it changes "24" into 24. I have a vector of sting type and I want to copy its content to a vector of int type. The aforesaid method is working perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1