当前位置: 首页>>代码示例>>C++>>正文


C++ std::count_if方法代码示例

本文整理汇总了C++中std::count_if方法的典型用法代码示例。如果您正苦于以下问题:C++ std::count_if方法的具体用法?C++ std::count_if怎么用?C++ std::count_if使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std的用法示例。


在下文中一共展示了std::count_if方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {

    cout << plus<int>()(3,4) << endl; // prints 7

    plus<int> intAdd;        // function object that can add two int values
    negate<int>  intNegate;  // function object that can negate an int value

    // uses intAdd::operator(int, int) to add 10 and 20
    int sum = intAdd(10, 20);        // sum = 30

    cout << sum << endl;

    // uses intNegate::operator(int) to generate -10 as second parameter
    // to intAdd::operator(int, int)
    sum = intAdd(10, intNegate(10));  // sum = 0

    cout << sum << endl;

    int arry[] = {0,1,2,3,4,5,16,17,18,19};

    vector<int> vec(arry, arry + 10);

    cout <<
    count_if(vec.begin(), vec.end(),
             bind2nd(less_equal<int>(), 10));
    cout << endl;

    cout <<
    count_if(vec.begin(), vec.end(),
             not1(bind2nd(less_equal<int>(), 10)));
    cout << endl;

    return 0;
}
开发者ID:CurvaVita,项目名称:C--------C---Primer--,代码行数:34,代码来源:fcnobj.cpp

示例2: main

int main() {

	cout << plus<int>()(3,4) << endl; // prints 7
	
	plus<int> intAdd;      // object that can add two int values
	negate<int> intNegate; // object that can negate an int value
	
	// uses intAdd::operator(int, int) to add 10 and 20
	int sum = intAdd(10, 20);         // equivalent to sum = 30
	cout << sum << endl;
	
	sum = intNegate(intAdd(10, 20));  // equivalent to sum = -30
	cout << sum << endl;
	
	// uses intNegate::operator(int) to generate -10 
	// as the second argument to intAdd::operator(int, int)
	sum = intAdd(10, intNegate(10));  // sum = 0
	
	cout << sum << endl;
	
#ifdef LIST_INIT
	vector<int> vec = {0,1,2,3,4,5,16,17,18,19};
#else
	int temp[] = {0,1,2,3,4,5,16,17,18,19};
	vector<int> vec(begin(temp), end(temp));
#endif
	
	// bind second argument to less_equal
	cout << count_if(vec.begin(), vec.end(),
		             bind(less_equal<int>(), _1, 10));  
	cout << endl;
	
	vector<string> svec;
	string in;
	while (cin >> in) 
		svec.push_back(in);

	function<decltype(size_compare)> fp1 = size_compare;

	//decltype(fp1)::result_type ret;
	function<bool(const string&)> fp2 = bind(size_compare, _1, 6);
	cout << count_if(svec.begin(), svec.end(), fp2)
	     << endl;
	cout << count_if(svec.begin(), svec.end(), 
	                 bind(size_compare, _1, 6))
	     << endl;


	return 0;
}
开发者ID:crayygy,项目名称:cpp-primer-notes,代码行数:50,代码来源:fcnobj.cpp

示例3: main

int main(void)
{
    string text = "the quick red fox jumps over the slow red turtle";
    istringstream is(text);
    vector<string> words;
    string s;

    while (is >> s)
        words.push_back(s);

    elimDups(words);    // put words into alphabetical order and remove duplicates

    for (const auto &s : words)
        cout << s << " ";
    cout << endl;

    // resort by length, maintaining alphabetical order among words of the same
    // length
    stable_sort(words.begin(), words.end(), isShorter);

    for (const auto &s : words)
        cout << s << " ";
    cout << endl;

    // how may words greater than length 6
    cout << count_if(words.begin(), words.end(),
            [](const string &s) { return s.size() > 6; })
         << " word(s) greater than length 6" << endl;

    return 0;
}
开发者ID:Cuculidae,项目名称:Learn,代码行数:31,代码来源:count_if.cpp

示例4: main

int main() {
  auto function = bind(longer_than_6, _1);
  vector<string> vec{"the",  "quick", "red",  "fox", "jumps",
                     "over", "the",   "slow", "red", "turtle"};
  cout << count_if(vec.begin(), vec.end(), function) << endl;
  return 0;
}
开发者ID:MisLink,项目名称:CppPrimer,代码行数:7,代码来源:ex22.cpp

示例5: failure_count

size_t test_suite::failure_count() const
{
  using std::count_if;
  using boost::bind;
  
  const size_t this_suite =
    static_cast<size_t>(
      count_if(
        _results.begin(),
        _results.end(),
        !bind(&test_result::success, _1)
      )
    );
  
  size_t children = 0;
  for (
    suite_map::const_iterator i = _suites.begin(), e = _suites.end();
    i != e;
    ++i
  )
  {
    children += i->second.failure_count();
  }
  
  return this_suite + children;
}
开发者ID:sabel83,项目名称:metatest,代码行数:26,代码来源:test_suite.cpp

示例6: Evaluate

float StageClassifier::Evaluate(vector<vector<vector<float>>>& X, vector<bool>& y)
{
    vector<float> probs;
    vector<float> TPRs, FPRs;
    float area = 0;

    for (int i = 0; i < X.size(); i++)
        probs.push_back(Predict(X[i]));

    /* test on training set (0.5 threshhold??) */
    //#if SETLEVEL == DEBUG_LEVEL
    //int TP = 0, TN = 0;
    //for (int i = 0; i < X.size(); i++)
    //{
    //    if ((probs[i] >= 0.5) && y[i] == true)
    //        TP++;
    //    else if ((probs[i] < 0.5) && y[i] == false)
    //        TN++;
    //}
    //LOG_DEBUG_NN("\t\tStrong classifier: ");
    //LOG_DEBUG_NN("TP = " << TP << '/' << y.size() / 2 << ", TN = " << TN << '/' << y.size() / 2);
    //LOG_DEBUG_NN(", Result: " << (float)(TP + TN) / y.size());
    //#endif

    for (float threshhold = 1; threshhold >= 0; threshhold -= auc_step)
    {
        TPRs.push_back(count_if(probs.begin(), probs.begin() + n_pos, bind2nd(greater_equal<float>(), threshhold)) / (float)n_pos);
        FPRs.push_back(count_if(probs.begin() + n_pos, probs.end(), bind2nd(greater_equal<float>(), threshhold)) / (float)n_neg);

        if (FPRs.size() > 1)
            area += (TPRs.back() + TPRs[TPRs.size() - 2]) * (FPRs.back() - FPRs[FPRs.size() - 2]) / 2; // trapezoid
            //area += TPRs[TPRs.size() - 2] * (FPRs.back() - FPRs[FPRs.size() - 2]); // left riemann sum
            //area += TPRs.back() * (FPRs.back() - FPRs[FPRs.size() - 2]); // right Riemann sum
    }

    return area;
}
开发者ID:mrgloom,项目名称:SurfCascade,代码行数:37,代码来源:StageClassifier.cpp

示例7: SearchTheta

void StageClassifier::SearchTheta(vector<vector<vector<float>>>& X, vector<bool>& y)
{
    int whole_n_total = (int)y.size();
    int whole_n_pos = (int)count(y.begin(), y.end(), true);
    int whole_n_neg = (int)(whole_n_total - whole_n_pos);

    vector<float> probs;

    for (int i = 0; i < X.size(); i++)
        probs.push_back(Predict(X[i]));

    /* search min TPR */
    float threshhold;
    for (threshhold = 1; threshhold >= 0; threshhold -= search_step)
    {
        TPR = count_if(probs.begin(), probs.begin() + whole_n_pos, bind2nd(greater_equal<float>(), threshhold)) / (float)whole_n_pos;
        if (TPR >= TPR_min)
            break;
    }

    /* get corresponding theta and FPR */
    theta = threshhold;
    FPR = count_if(probs.begin() + whole_n_pos, probs.end(), bind2nd(greater_equal<float>(), threshhold)) / (float)whole_n_neg;
}
开发者ID:mrgloom,项目名称:SurfCascade,代码行数:24,代码来源:StageClassifier.cpp

示例8: main

int main()
{
	vector<int> ivec{1, 16, 256, 4096, 65536};
	vector<string> svec{"pooh", "pooh", "pooh", "hoop", "pooh"};

	auto cnt = count_if(ivec.cbegin(), ivec.cend(), bind(greater<int>(), _1, 1024));
	cout << cnt << endl;
	auto it = find_if(svec.cbegin(), svec.cend(), bind(not_equal_to<string>(), _1, "pooh"));
	cout << *it << endl;
	transform(ivec.begin(), ivec.end(), ivec.begin(), bind(multiplies<int>(), _1, 2));
	for (const auto &val : ivec)
		cout << val << ' ';

	return 0;
}
开发者ID:coder-e1adbc,项目名称:CppPrimer,代码行数:15,代码来源:ex42.cpp

示例9: main

int main(int argc, char* argv[])
{
    ifstream file(argv[1]);
    string word;
    vector<string> text;
    while (file >> word)
    {
        text.push_back(word);
    }
    for (auto i = 0; i < 50;++i)
    {
        if(auto n = count_if(text.cbegin(), text.cend(), Boundry(i)))
        std::cout << i << " length words has" << n << "." << std::endl;
    }

    return 0;
}
开发者ID:Pt0lemaeus,项目名称:C-Primer5Edition,代码行数:17,代码来源:exercise_14.39.cpp

示例10: main

int main(int argc, char *argv[])
{
    double duty = 0.5;
    if (argc > 1)
    {
        duty = std::atof(argv[1]);
    }

    // sample frequency is fixed at 1 kHz, signal frequency at 10 Hz
    Aquila::SquareGenerator gen(1000);
    gen.setDuty(duty).setAmplitude(1).setFrequency(10).generate(1000);

    // prints number of positive samples in generated signal
    std::cout << count_if(gen.begin(), gen.end(),
                          bind2nd(greater<Aquila::SampleType>(), 0));

    return 0;
}
开发者ID:sequoiar,项目名称:aquila,代码行数:18,代码来源:sg_duty_cycle.cpp

示例11: main

int main()
{
    list<string> words;
    string next_word;

    while (cin >> next_word)
        words.push_back(next_word);

    words.sort();
    print_words(words);

    words.unique();
    print_words(words);

    list<string>::size_type wc = count_if(words.begin(), words.end(), GT6);
    cout << wc << " words 6 characters or longer" << endl;
    return 0;
}
开发者ID:leonshao,项目名称:cplusplus,代码行数:18,代码来源:sort_list.cpp

示例12: main

int main() 
{
	vector<int> iVector{ 1, 22, 3434, 83448, 83781 }, iRet;
	// cout the int which is bigger than 1024 using function class
	int count = count_if(iVector.begin(), iVector.end(), bind(std::greater<int>(), _1, 1024));

	vector<string> sVector{"pooh", "hello", "pooh", "world"};
	// find the first string not equal to "pooh"
	vector<string>::iterator position = find_if(sVector.begin(), sVector.end(), bind(std::not_equal_to<string>(), _1, "pooh"));

	// multiplies each element in iVector, and put the result to iRet;
	std::transform(iVector.begin(), iVector.end(), back_inserter(iRet), bind(std::multiplies<int>(), _1, 2));

	cout << "cout : " << count << endl;
	cout << "The first string : " << *position << endl;
	for (const auto& item: iRet) 
	{
		cout << item << " ";
	}
	cout << endl;
	return 0;
}
开发者ID:PraflyGod,项目名称:Cpp,代码行数:22,代码来源:Exercise14.42.cpp

示例13: count_if

//! Exercise 10.20
std::size_t bigerThan6(vector<string> const& v)
{
    return count_if(v.cbegin(), v.cend(), [](string const& s){
        return s.size() > 6;
    });
}
开发者ID:AllyW,项目名称:Cpp-Primer,代码行数:7,代码来源:ex10.20_21.cpp

示例14: insertElements

  insertElements(con, 1, 10);
  printContainer(con, "con: ");  // con: 1 2 3 4 5 6 7 8 9 10

  int numOf1 = count(con.begin(), con.end(), 1);
  psln(numOf1);
  EXPECT_EQ(1, numOf1);      // one 1.
  con.push_back(1);
  con.push_back(1);
  printContainer(con, "con: ");  // con: 1 2 3 4 5 6 7 8 9 10 1 1
  numOf1 = count(con.begin(), con.end(), 1);
  psln(numOf1);
  EXPECT_EQ(3, numOf1);      // three 1.

  using std::count_if;
  int numOfEven = count_if(con.begin(), con.end(),
    [](int elem) -> bool {
    return ( elem % 2 == 0 );     // 2 4 6 8 10 -> five even.
  });
  psln(numOfEven);
  EXPECT_EQ(5, numOfEven);

END_TEST;




bool absLess(int a, int b) {
  return abs(a) < abs(b);
}


开发者ID:adan830,项目名称:CS.cpp,代码行数:29,代码来源:nonmodifying_algorithms.cpp


注:本文中的std::count_if方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。