#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"; }
所有评论(0)