Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++;? 我对C++很陌生,我在努力解决以下问题: 我正在解析来自iptables的系统日志消息。每条消息看起来都像: 192.168.1.1:20200:Dec 11 15:20:36 SRC=192.168.1.5 DST=8.8.8 LEN=250 我需要快速地(因为新消息来得非常快)解析字符串以获得SRC、DST和LEN。 如果它是一个简单的程序,我会使用std::find来查找STR子字符串的索引,然后在一个循环中将下一个字符添加到数组中,直到遇到空白。然后我会对DST和LEN执行相同的操作 比如说, std::string x = "15:30:20 SRC=192.168.1.1 DST=15.15.15.15 LEN=255"; std::string substr; std::cout << "Original string: \"" << x << "\"" << std::endl; // Below "magic number" 4 means length of "SRC=" string // which is the same for "DST=" and "LEN=" // For SRC auto npos = x.find("SRC"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos+4)); std::cout << "SRC: " << substr << std::endl; } // For DST npos = x.find("DST"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos + 4)); std::cout << "DST: " << substr << std::endl; } // For LEN npos = x.find("LEN"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find('\0', npos) - (npos + 4)); std::cout << "LEN: " << substr << std::endl; } std::string x=“15:30:20 SRC=192.168.1.1 DST=15.15.15 LEN=255”; std::字符串substr; std::cout_C++_String_Substring - Fatal编程技术网

如何在C++;? 我对C++很陌生,我在努力解决以下问题: 我正在解析来自iptables的系统日志消息。每条消息看起来都像: 192.168.1.1:20200:Dec 11 15:20:36 SRC=192.168.1.5 DST=8.8.8 LEN=250 我需要快速地(因为新消息来得非常快)解析字符串以获得SRC、DST和LEN。 如果它是一个简单的程序,我会使用std::find来查找STR子字符串的索引,然后在一个循环中将下一个字符添加到数组中,直到遇到空白。然后我会对DST和LEN执行相同的操作 比如说, std::string x = "15:30:20 SRC=192.168.1.1 DST=15.15.15.15 LEN=255"; std::string substr; std::cout << "Original string: \"" << x << "\"" << std::endl; // Below "magic number" 4 means length of "SRC=" string // which is the same for "DST=" and "LEN=" // For SRC auto npos = x.find("SRC"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos+4)); std::cout << "SRC: " << substr << std::endl; } // For DST npos = x.find("DST"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos + 4)); std::cout << "DST: " << substr << std::endl; } // For LEN npos = x.find("LEN"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find('\0', npos) - (npos + 4)); std::cout << "LEN: " << substr << std::endl; } std::string x=“15:30:20 SRC=192.168.1.1 DST=15.15.15 LEN=255”; std::字符串substr; std::cout

如何在C++;? 我对C++很陌生,我在努力解决以下问题: 我正在解析来自iptables的系统日志消息。每条消息看起来都像: 192.168.1.1:20200:Dec 11 15:20:36 SRC=192.168.1.5 DST=8.8.8 LEN=250 我需要快速地(因为新消息来得非常快)解析字符串以获得SRC、DST和LEN。 如果它是一个简单的程序,我会使用std::find来查找STR子字符串的索引,然后在一个循环中将下一个字符添加到数组中,直到遇到空白。然后我会对DST和LEN执行相同的操作 比如说, std::string x = "15:30:20 SRC=192.168.1.1 DST=15.15.15.15 LEN=255"; std::string substr; std::cout << "Original string: \"" << x << "\"" << std::endl; // Below "magic number" 4 means length of "SRC=" string // which is the same for "DST=" and "LEN=" // For SRC auto npos = x.find("SRC"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos+4)); std::cout << "SRC: " << substr << std::endl; } // For DST npos = x.find("DST"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find(" ", npos) - (npos + 4)); std::cout << "DST: " << substr << std::endl; } // For LEN npos = x.find("LEN"); if (npos != std::string::npos) { substr = x.substr(npos + 4, x.find('\0', npos) - (npos + 4)); std::cout << "LEN: " << substr << std::endl; } std::string x=“15:30:20 SRC=192.168.1.1 DST=15.15.15 LEN=255”; std::字符串substr; std::cout,c++,string,substring,C++,String,Substring,“快速,理想情况下在一次迭代中”-实际上,程序的速度并不取决于源代码中可见的循环数。特别是正则表达式是隐藏多个嵌套循环的非常好的方法 你的解决方案其实相当不错。在查找“SRC”之前,它不会浪费太多时间,并且不会在检索IP地址所需的范围之外进行搜索。当然,当搜索“SRC”时,它在“Sep”的第一个“S”上有一个假阳性,但下一次比较解决了这个问题。如果您确定第一次出现“SRC”是在第20列的某个地方,那么跳过前20个字符可能只会节省一点点速度。(检查您的日志,我说不出来)“快速,最好是一次迭代”——

“快速,理想情况下在一次迭代中”-实际上,程序的速度并不取决于源代码中可见的循环数。特别是正则表达式是隐藏多个嵌套循环的非常好的方法

你的解决方案其实相当不错。在查找“SRC”之前,它不会浪费太多时间,并且不会在检索IP地址所需的范围之外进行搜索。当然,当搜索“SRC”时,它在“Sep”的第一个“S”上有一个假阳性,但下一次比较解决了这个问题。如果您确定第一次出现“SRC”是在第20列的某个地方,那么跳过前20个字符可能只会节省一点点速度。(检查您的日志,我说不出来)

“快速,最好是一次迭代”——实际上,程序的速度并不取决于源代码中可见的循环数。特别是正则表达式是隐藏多个嵌套循环的非常好的方法


你的解决方案其实相当不错。在查找“SRC”之前,它不会浪费太多时间,并且不会在检索IP地址所需的范围之外进行搜索。当然,当搜索“SRC”时,它在“Sep”的第一个“S”上有一个假阳性,但下一次比较解决了这个问题。如果您确定第一次出现“SRC”是在第20列的某个地方,那么跳过前20个字符可能只会节省一点点速度。(检查您的日志,我看不出来)

您可以使用
std::regex
,例如:

std::string x = "15:30:20 SRC=192.168.1.1 DST=15.15.15.15 LEN=255";

std::regex const r(R"(SRC=(\S+) DST=(\S+) LEN=(\S+))");
std::smatch matches;
if(regex_search(x, matches, r)) {
    std::cout << "SRC " << matches.str(1) << '\n';
    std::cout << "DST " << matches.str(2) << '\n';
    std::cout << "LEN " << matches.str(3) << '\n';
}
std::string x=“15:30:20 SRC=192.168.1.1 DST=15.15.15 LEN=255”;
std::regex const r(r)(SRC=(\S+)DST=(\S+)LEN=(\S+);
std::smatch匹配;
if(正则表达式搜索(x,匹配项,r)){

std::cout您可以使用
std::regex
,例如:

std::string x = "15:30:20 SRC=192.168.1.1 DST=15.15.15.15 LEN=255";

std::regex const r(R"(SRC=(\S+) DST=(\S+) LEN=(\S+))");
std::smatch matches;
if(regex_search(x, matches, r)) {
    std::cout << "SRC " << matches.str(1) << '\n';
    std::cout << "DST " << matches.str(2) << '\n';
    std::cout << "LEN " << matches.str(3) << '\n';
}
std::string x=“15:30:20 SRC=192.168.1.1 DST=15.15.15 LEN=255”;
std::regex const r(r)(SRC=(\S+)DST=(\S+)LEN=(\S+);
std::smatch匹配;
if(正则表达式搜索(x,匹配项,r)){

std::cout如果您的格式是固定的并经过验证的(只要输入字符串不完全包含期望的字符,您就可以接受未定义的行为),那么您可能会通过手工编写较大的部分来挤出一些性能,并跳过所有标准函数中的字符串终止测试

// buf_ptr will be updated to point to the first character after the " SRC=x.x.x.x" sequence
unsigned long GetSRC(const char*& buf_ptr)
{
    // Don't search like this unless you have a trusted input format that's guaranteed to contain " SRC="!!!
    while (*buf_ptr != ' ' ||
        *(buf_ptr + 1) != 'S' ||
        *(buf_ptr + 2) != 'R' ||
        *(buf_ptr + 3) != 'C' ||
        *(buf_ptr + 4) != '=') 
    {
        ++buf_ptr;
    }
    buf_ptr += 5;
    char* next;

    long part = std::strtol(buf_ptr, &next, 10);
    // part is now the first number of the IP. Depending on your requirements you may want to extract the string instead
    unsigned long result = (unsigned long)part << 24;

    // Don't use 'next + 1' like this unless you have a trusted input format!!!
    part = std::strtol(next + 1, &next, 10);
    // part is now the second number of the IP. Depending on your requirements ...
    result |= (unsigned long)part << 16;

    part = std::strtol(next + 1, &next, 10);
    // part is now the third number of the IP. Depending on your requirements ...
    result |= (unsigned long)part << 8;

    part = std::strtol(next + 1, &next, 10);
    // part is now the fourth number of the IP. Depending on your requirements ...
    result |= (unsigned long)part;

    // update the buf_ptr so searching for the next information ( DST=x.x.x.x) starts at the end of the currently parsed parts
    buf_ptr = next;
    return result;
}
//buf_ptr将更新为指向“SRC=x.x.x.x”序列后的第一个字符
无符号长GetSRC(常量字符*&buf_ptr)
{
//除非您有保证包含“SRC=”的可信输入格式,否则不要这样搜索!!!
而(*buf_ptr!=“”)||
*(buf_ptr+1)!='S'||
*(buf_ptr+2)!=“R”||
*(buf_ptr+3)!=“C”||
*(buf_ptr+4)!
{
++buf_ptr;
}
buf_ptr+=5;
char*next;
长部件=标准::strtol(buf_ptr和next,10);
//part现在是IP的第一个数字。根据您的要求,您可能希望提取字符串

unsigned long result=(unsigned long)part如果您的格式是固定的并经过验证的(只要输入字符串不完全包含所需的字符,您就可以接受未定义的行为),然后您可以通过手工编写较大的部分来挤出一些性能,并跳过所有标准函数中的字符串终止测试

// buf_ptr will be updated to point to the first character after the " SRC=x.x.x.x" sequence
unsigned long GetSRC(const char*& buf_ptr)
{
    // Don't search like this unless you have a trusted input format that's guaranteed to contain " SRC="!!!
    while (*buf_ptr != ' ' ||
        *(buf_ptr + 1) != 'S' ||
        *(buf_ptr + 2) != 'R' ||
        *(buf_ptr + 3) != 'C' ||
        *(buf_ptr + 4) != '=') 
    {
        ++buf_ptr;
    }
    buf_ptr += 5;
    char* next;

    long part = std::strtol(buf_ptr, &next, 10);
    // part is now the first number of the IP. Depending on your requirements you may want to extract the string instead
    unsigned long result = (unsigned long)part << 24;

    // Don't use 'next + 1' like this unless you have a trusted input format!!!
    part = std::strtol(next + 1, &next, 10);
    // part is now the second number of the IP. Depending on your requirements ...
    result |= (unsigned long)part << 16;

    part = std::strtol(next + 1, &next, 10);
    // part is now the third number of the IP. Depending on your requirements ...
    result |= (unsigned long)part << 8;

    part = std::strtol(next + 1, &next, 10);
    // part is now the fourth number of the IP. Depending on your requirements ...
    result |= (unsigned long)part;

    // update the buf_ptr so searching for the next information ( DST=x.x.x.x) starts at the end of the currently parsed parts
    buf_ptr = next;
    return result;
}
//buf_ptr将更新为指向“SRC=x.x.x.x”序列后的第一个字符
无符号长GetSRC(常量字符*&buf_ptr)
{
//除非您有保证包含“SRC=”的可信输入格式,否则不要这样搜索!!!
而(*buf_ptr!=“”)||
*(buf_ptr+1)!='S'||
*(buf_ptr+2)!=“R”||
*(buf_ptr+3)!=“C”||
*(buf_ptr+4)!
{
++buf_ptr;
}
buf_ptr+=5;
char*next;
长部件=标准::strtol(buf_ptr和next,10);
//part现在是IP的第一个数字。根据您的要求,您可能希望提取字符串

unsigned long result=(unsigned long)关于正则表达式的一点警告:有句谚语说“你有一个问题。你用正则表达式解决它。现在你有两个问题”。虽然正则表达式可以是一个强大的工具,但它也非常先进,绝对不平凡。正则表达式很容易出错,只能作为最后的努力使用。尤其是初学者,甚至是中级程序员。你有证据表明你的程序太慢吗?关于正则表达式的一点警告pressions:有句谚语说“你有一个问题。你用正则表达式解决它。现在你有两个问题”。虽然正则表达式可以是一个强大的工具,但它也非常先进,绝对不平凡。正则表达式很容易出错,只能作为最后的努力使用。尤其是初学者,甚至是中级程序员。你有证据表明你的程序太慢了吗?@Groosha我想知道,你有没有性能测试从问题中测试您以前的解决方案,并将其与正则表达式解决方案进行比较?我感兴趣的是您是否真的从正则表达式中获得了任何性能,还是仅仅获得了可维护代码的美。@Groosha您真的应该进行测试