简体   繁体   中英

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

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 . Can someone help me? I know, there are several ways to solve this problem by using if conditions and loops. However, my question is related to the InputIterators (InputIterator first, InputIterator last):

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

How do I have to set the InputIterators to consider only the nth column? The 2d vector is huge, this is the reason, why I don't want to check the entire 2d vector.

Seems like there is no such solution with InputIterators . Hence, here is the solution with if condition and for loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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