E005

向量中的find()函数,用于找到符合条件的索引。支持关系运算和逻辑运算表达式。

 

#include "orsci.h"
using namespace orsci;


int main()
{
{
vdouble x;
x = "-5:10, 7, 8";
cout << x << endl; //-5,-4,...,8, 9, 10, 7, 8
cout << x.find(5) << endl; //返回元素5的各个索引。
cout << x.find(vmt::GT(0)) << endl; //GT(0)代表大于0的索引
cout << x.find(vmt::BETWEEN(3, 7)) << endl; //返回处于3和7之间的索引
cout << x.find(vmt::NOT(vmt::EQU(7))) << endl; //不等于7的元素索引,类似x.sum(vmt::NoEQU(7)。
cout << x.find(vmt::OR(vmt::LS(0), vmt::BETWEEN(3, 7))) << endl; //返回<0或处于[3,7]区间的数索引
cout << x.find(vmt::AND(vmt::BETWEEN(3, 7), vmt::NoEQU(5))) << endl; //返回3到7之间但不是5的数索引
//支持复杂逻辑表达式
cout << x.find(vmt::OR(vmt::AND(vmt::GT(3), vmt::LS(7)), vmt::NOT(vmt::NOT(vmt::EQU(1))))) << endl; //计算(>3 and <7 ) or (==1)的索引
//可配合subvector(...)或view(..)使用。例如:
vint index = x.find(vmt::AND(vmt::BETWEEN(3, 7), vmt::NoEQU(5)));
cout << x.subvector(index) << endl; //利用索引返回向量。结果为:3 4 6 7 7
cout << x.view(index) << endl; //利用索引返回向量。结果为:3 4 6 7 7
vdouble greatZero = x.subvector(x.find(vmt::GT(0))); //大于零的所有元素。
cout << greatZero << endl; //结果:1 2 3 4 5 6 7 8 9 10 7 8
}
return 0;
}

输出

(一)程序运行结果

-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 7 8
0 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17
8 9 10 11 12 16
0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 17
0 1 2 3 4 8 9 10 11 12 16
8 9 11 12 16
6 9 10 11
3 4 6 7 7
1 2 3 4 5 6 7 8 9 10 7 8

(二)说明:

(1)vdouble、vint等向量都支持带有关系运算和逻辑运算的复杂表达式形式,例如sum()、count、find()、minv()、maxv()、mean()、var()、var_pop()、stddev()、stddev_pop()等。

(2)find的结果经常配合subvector(...)或view(...)使用。

书籍 《数据分析与数据挖掘》、《数据分析与数据挖掘建模与工具》、《文本分析与文本挖掘》。
软件 orsci开发包(C++语言)。