C++ 如何对向量中的字符串进行模式匹配?
我有一个向量中的数据,我需要看看第三个元素vrecord[2]中是否有Buy或Sell这个词 查找向量中字符串出现的最直接的方法是什么 数据: 代码: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
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请阅读您自己编写的内容:“我有向量中的数据,我需要看看它是否有“买入”或“卖出”这个词”