Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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++_Regex_Vector - Fatal编程技术网

C++ 如何对向量中的字符串进行模式匹配?

C++ 如何对向量中的字符串进行模式匹配?,c++,regex,vector,C++,Regex,Vector,我有一个向量中的数据,我需要看看第三个元素vrecord[2]中是否有Buy或Sell这个词 查找向量中字符串出现的最直接的方法是什么 数据: 代码: vectorvrecords; while(std::fgets(buff,sizeof buff,fp)!=NULL){ vrecords.向后推(buff); } 对于(int t=0;t代码>和/或获取 >类:代码:ST: 考虑到C函数fgets也将新行字符存储在字符串中。您应该删除它。例如 while ( std::fgets( buff

我有一个向量中的数据,我需要看看第三个元素vrecord[2]中是否有Buy或Sell这个词

查找向量中字符串出现的最直接的方法是什么

数据:

代码:

vectorvrecords;
while(std::fgets(buff,sizeof buff,fp)!=NULL){
vrecords.向后推(buff);
}
对于(int t=0;t首先,使用C++中的C++ I/O系统是一个坏主意。最好使用C++函数<代码> STD::GETLION<代码>或成员函数<代码> GETLION>代码>和/或<代码>获取<代码> >类:代码:ST:
考虑到C函数
fgets
也将新行字符存储在字符串中。您应该删除它。例如

while ( std::fgets( buff, sizeof buff, fp ) != NULL )
{
    size_t n = std::strlen( buff );
    if ( n && buff[n-1] == '\n' ) buff[n-1] = '\0';    
    if ( buff[0] != '\0' ) vrecords.push_back( buff );
}
#include <algorithm>
#include <iterator>

//...

auto it = std::find( vrecords.begin(), vrecords.end(), "Buy" );

if ( it != vrecords.end() ) 
{
    std::cout << "Word \"" << "Buy"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
#include <algorithm>
#include <iterator>

//...

const char * s[] = { "Buy", "Sell" };

auto it = std::find_first_of( vrecords.begin(), vrecords.end(), 
                              std::begin( s ), std::end( s ) );

if ( it != vrecords.end() ) 
{
    std::cout << "One of the words \"" << "Buy and Sell"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
const char * s[] = { "Buy", "Sell" };

auto n = std::count_if( vrecords.begin(), vrecords.end(),
                        [&]( const std::string &record )
                        { 
                            return record == s[0] || record == s[1];
                        } );
如果向量声明为
std::vector
(我希望它不是声明为例如
std::vector
),那么您可以改为编写

std::string record;
while ( std::getline( YourFileStream, record ) )
{
    if ( !record.empty() ) vrecords.push_back( record );
}
在这种情况下,使用标题
中声明的标准算法
std::find
查找单词“Buy”很简单

while ( std::fgets( buff, sizeof buff, fp ) != NULL )
{
    size_t n = std::strlen( buff );
    if ( n && buff[n-1] == '\n' ) buff[n-1] = '\0';    
    if ( buff[0] != '\0' ) vrecords.push_back( buff );
}
#include <algorithm>
#include <iterator>

//...

auto it = std::find( vrecords.begin(), vrecords.end(), "Buy" );

if ( it != vrecords.end() ) 
{
    std::cout << "Word \"" << "Buy"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
#include <algorithm>
#include <iterator>

//...

const char * s[] = { "Buy", "Sell" };

auto it = std::find_first_of( vrecords.begin(), vrecords.end(), 
                              std::begin( s ), std::end( s ) );

if ( it != vrecords.end() ) 
{
    std::cout << "One of the words \"" << "Buy and Sell"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
const char * s[] = { "Buy", "Sell" };

auto n = std::count_if( vrecords.begin(), vrecords.end(),
                        [&]( const std::string &record )
                        { 
                            return record == s[0] || record == s[1];
                        } );
如果需要计算向量中有多少这样的单词,那么可以在循环中使用上述方法,或者使用标准算法
std::count
std::count\u If
std::acculate
或基于范围的for循环。 比如说

while ( std::fgets( buff, sizeof buff, fp ) != NULL )
{
    size_t n = std::strlen( buff );
    if ( n && buff[n-1] == '\n' ) buff[n-1] = '\0';    
    if ( buff[0] != '\0' ) vrecords.push_back( buff );
}
#include <algorithm>
#include <iterator>

//...

auto it = std::find( vrecords.begin(), vrecords.end(), "Buy" );

if ( it != vrecords.end() ) 
{
    std::cout << "Word \"" << "Buy"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
#include <algorithm>
#include <iterator>

//...

const char * s[] = { "Buy", "Sell" };

auto it = std::find_first_of( vrecords.begin(), vrecords.end(), 
                              std::begin( s ), std::end( s ) );

if ( it != vrecords.end() ) 
{
    std::cout << "One of the words \"" << "Buy and Sell"
              << "\" is found at position " 
              << std::distance( vrecords.begin(), it )
              << std::endl;  
}
const char * s[] = { "Buy", "Sell" };

auto n = std::count_if( vrecords.begin(), vrecords.end(),
                        [&]( const std::string &record )
                        { 
                            return record == s[0] || record == s[1];
                        } );

如果(vrecords[2]==“Buy”| | vrecords[2]==“Sell”)
?在我看来,regex会有点过分。@bro显示向量是如何定义的。vector vrecords;您的代码显示1次购买,但它应该显示3次。该数据流中有3次购买。(@bro请阅读您自己编写的内容:“我有向量中的数据,我需要看看它是否有“买入”或“卖出”这个词”