c++ - 在字符串 vector 中创建 20,000 个 MAC 地址

标签 c++ mac-address

我需要创建一个 MAC 地址的字符串 vector ,从 1000.1111.1111 到 1000.1131.1111 或类似的范围。

我不确定如何增加字符串,或者如果我连接然后如何保持前导零。

感谢任何指点。

是的,这些是十六进制。尽管我不介意只处理基数 10 的解决方案。

最佳答案

这将生成一个字符串 vector ,如下所示:

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

代码:

#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);
        }
}

关于c++ - 在字符串 vector 中创建 20,000 个 MAC 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517530/

相关文章:

python - 正则表达式匹配 MAC 地址

c++ - 生成文件错误

python - 使用 Kivy 创建仿真器前端是否可行?

c++ - 时钟获取时间 : identifier not found in Visual Studio in Windows 10

c++ - 在 C++ 中使用 DirectShow 或其他 COM 库时,我#import 哪些 DLL?

iOS - 从 CBPeripheral 对象获取外设的 MAC 地址

c++ - 从模态对话框中退出 MFC 应用程序

php - 如何在 laravel 中获取客户端 IP 地址和 MAC 地址?

ruby-on-rails - 通过连接到 Ruby on Rails 应用程序更新主机名?

ios5 - 推送通知使用的 monotouch Iphone 唯一标识符或 MAC 地址