在C++中,切片通常是使用std::vector
或者std::array
来实现的。对于std::vector
,可以使用size()
方法来获取切片的长度,使用capacity()
方法来获取切片的容量。示例如下:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::cout << "Length: " << numbers.size() << std::endl;
std::cout << "Capacity: " << numbers.capacity() << std::endl;
return 0;
}
对于std::array
,由于其固定大小,其长度即为其容量。示例如下:
#include <iostream>
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
std::cout << "Length: " << numbers.size() << std::endl;
std::cout << "Capacity: " << numbers.size() << std::endl;
return 0;
}