简体   繁体   English

C ++:在2d向量的特定列中计数

[英]C++: Count in specific column of 2d vector

The following code counts how many times the string "CMO" occurs in the nth row of the 2d vector dataIn 以下代码计算字符串“ CMO”在二维矢量数据的第n行中出现的次数

vector< vector<string> > dataIn ( nrows, vector<string> (ncolumns) );
int mycount = count(dataIn[n].begin(), dataIn[n].end(), "CMO");

Now, I would like to do the same thing, however, in the nth column . 现在,我想在第n栏中做同样的事情。 Can someone help me? 有人能帮我吗? I know, there are several ways to solve this problem by using if conditions and loops. 我知道,有几种方法可以通过使用if条件和循环来解决此问题。 However, my question is related to the InputIterators (InputIterator first, InputIterator last): 但是,我的问题与InputIterators有关(首先是InputIterator,最后是InputIterator):

count (InputIterator first, InputIterator last, const T& val)

How do I have to set the InputIterators to consider only the nth column? 我如何设置InputIterators仅考虑第n列? The 2d vector is huge, this is the reason, why I don't want to check the entire 2d vector. 2d向量很大,这就是为什么我不想检查整个2d向量的原因。

Seems like there is no such solution with InputIterators . 似乎InputIterators没有这种解决方案。 Hence, here is the solution with if condition and for loop: 因此,这是if条件和for循环的解决方案:

unsigned int c = 0;
for(auto vec : dataIn) {
  if(vec[n] == "CMO") {
    c += 1;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM