Thread: Using Auto in a for loop

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    Using Auto in a for loop

    Hi all,

    i was doing the following:
    Code:
    for (int i = 0; i < numbers.capacity(); i++) {
            numbers.push_back(rand() % 30);
    }
    so i got the warning:
    Code:
    warning C4018: '<': signed/unsigned mismatch
    i get the same warning if i use "auto" instead of "int" :
    Code:
    for (auto i = 0; i < numbers.capacity(); i++) {
            numbers.push_back(rand() % 30);
    }
    i get only rid of the warning when i use "unsigned int".

    My question:
    itsn't that where "auto" is supposed to help? so that i don't need to bother aboutthe data type ?
    what i am i doing wrong? is there some best practice where not to use "auto" ?

    i used a lot with range based for loops, it works fine

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The type of 0 is int, so using auto here still causes i to be of type int. If you really wanted to, you could write:
    Code:
    for (decltype(numbers.capacity()) i = 0; i < numbers.capacity(); i++)
    But that seems unnecessarily repetitive to me. Maybe:
    Code:
    auto capacity = numbers.capacity();
    for (decltype(capacity) i = 0; i < capacity; ++i)
    EDIT:
    I might test this later to be sure, but the thought came to mind that you might be able to use generate_n with back_inserter to accomplish this:
    Code:
    std::generate_n(std::back_inserter(numbers),
                    numbers.capacity(),
                    []() { return rand() % 30; });
    Last edited by laserlight; 10-11-2020 at 05:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    The type of 0 is int, so using auto here still causes i to be of type int
    alright alright, now i got it, and thanks for the alternatives.

    generate_n with back_inserter are very new to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2013, 05:56 PM
  2. Auto-response
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2006, 03:51 PM
  3. auto log in program ....
    By twomers in forum C++ Programming
    Replies: 15
    Last Post: 01-16-2006, 06:39 AM
  4. auto?
    By cboard_member in forum C Programming
    Replies: 9
    Last Post: 10-07-2005, 12:58 AM
  5. Auto continue
    By cyberCLoWn in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2004, 02:58 PM

Tags for this Thread