c++ 判断容器是否为空

 

#include 
#include 
#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";
}

 

转载于:https://www.cnblogs.com/sea-stream/p/10110747.html

你可能感兴趣的:(c++ 判断容器是否为空)