Search code examples
c++mac-address

Creating 20,000 MAC addresses in a string vector


I need to create a string vector of MAC addresses from 1000.1111.1111 to 1000.1131.1111 or a range similar to that.

I am not sure how to increment the string, or if I concatenate then how to maintain leading zeros.

Any pointers are appreciated.

Yes these are hex. Although I would not mind a solution that takes care of base 10 only.


Solution

  • This will generate a vector of strings like this:

    1000.1111.1111
    1000.1111.1112
    1000.1111.1113
    <...>
    1000.1112.1111
    1000.1112.1112
    <...>
    1000.1131.1111
    

    Code:

    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    //Converts a number (in this case, int) to a string
    string convertInt(int number)
    {
       stringstream ss;//create a stringstream
       ss << number;//add number to the stream
       return ss.str();//return a string with the contents of the stream
    }
    
    int main(int argc, char *argv[])
    {
        //The result vector
        vector<string> result;
        string tmp;//The temporary item 
        for( int i = 1111; i < 1139; i++ )
            for( int j = 1111; j < 9999; j++ )
            {
                tmp = "1000.";//the base of the adress
                //Now we append the second and the third numbers.
                tmp.append( convertInt( i ) ).append( "." ).append( convertInt( j ) );
                //and add the tmp to the vector
                result.push_back(tmp);
            }
    }