C++中如何获取vector数组中每个string类型元素的长度
代码示例
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> strVector = {"Hello", "World", "HappyBirthday", "12345"};
for (const std::string& str : strVector) {
std::cout << "Size of '" << str << "' is: " << str.size() << " characters" << std::endl;
}
return 0;
}
运行结果
Size of 'Hello' is: 5 characters
Size of 'World' is: 5 characters
Size of 'HappyBirthday' is: 13 characters
Size of '12345' is: 5 characters