Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4124070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:41:35+00:00 2026-05-20T23:41:35+00:00

I have an application where I get a vector<string> . I need to iterate

  • 0

I have an application where I get a vector<string>. I need to iterate through each element in the vector and see if a value is an integer value.

Although the vector represents strings, few of the elements can contain an integer. I need to figure out which of those elements are integers, and if an element is an integer, I need its value. If an element in the vector is a string, then I just ignore it.

I tried to use atoi(vector[index].c_str()), but I have an issue with it. atoi returns an integer value if the value contained in the string is an integer. If not, it returns 0

So, consider the following:

atoi("Shankar") = 0
atoi("0") = 0

and

atoi("123") = 123
atoi("123Shankar") = 123

So, how do I distinguish between the above shown cases? If this cannot be achieved using atoi, then what is the alternate solution to this problem?

Please assist.

EDIT:

I can loop through the string and see if every character is an integer, but that reduces performance, since for m strings with an average of n characters, I need to check m X n times which makes it O(n^2).

is there a better way to solve this problem?

EDIT2:

Unfortunately, I cannot use any 3rd party library for this and just use STL

EDIT3:

In my application, the vector does not contain any negative integers so I am considering Xeo’s solution since sstream does not distinguish between “123” and “123Shankar”

Thanks everyone for your assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:41:35+00:00Added an answer on May 20, 2026 at 11:41 pm

    Just go through your string and check every character if it’s an integer. If not, break out and report false.

    bool IsDigit(char c){
      return '0' <= c && c <= '9';
    }
    
    bool IsInteger(std::string const& str){
      size_t i = 0;
      if(*str == '-') ++i;
      for( ; i < str.size(); ++i){
         if(!IsDigit(str[i]))
           return false;
      }
      // all chars are integers
      return true;
    }
    

    Edit
    atoi doesn’t really do anything else. See this example implementation:

    int StrToInt(char const* str){
      int ret = 0, sign = 1;
      if(*str == '-'){
        sign = -1;
        ++str;
      }
      while(IsDigit(*str)){
        ret *= 10; // make room for the next digit
        ret += ((*str) - 0x30); // convert char to digit
        ++str;
      }
      return ret * sign;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Php application using stream_socket_client(), to get data through tcp from a
I have a visual studio 2008 C++ application where I need to get information
I have an application which get copied and run on client machines. The program
I have an application where the contents of e-mails that get sent are stored
I have an application that needs to hit the ActiveDirectory to get user permission/roles
OK, struggling to get a definitive answer to this one... I have an application
I have a winform application that uses some referenced web services to get data.
Now that I know C++ I want to get into desktop application that have
We have a project with many dll files which get loaded when the application
We're trying to get SMB volume listings in our OS X application, and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.