c++ 判断容器是否为空

 

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<int> ints {1, 2, 4};
    vector<string> fruits {"orange", "apple", "raspberry"};
    vector<char> empty;
    
    // Sums all integers in the vector ints (if any), printing only the result.
    int sum = 0;
    for (auto it = ints.cbegin(); it != ints.cend(); it++)
        sum += *it;
    cout << "Sum of ints: " << sum << "\n";
    
    // 输出容器的第一个元素
    cout << "First fruit: " << *fruits.begin() << "\n";
    
    if (empty.begin() == empty.end())
        cout << "vector 'empty' is indeed empty.\n";
}

 

posted @ 2018-12-12 20:17  anobscureretreat  阅读(1560)  评论(0编辑  收藏  举报