面试题答案
一键面试- 实现思路:
- C++是强类型语言,
vector<int>
定义后,编译器会根据类型信息进行检查。当试图插入非int
类型数据时,编译器会报错,从而确保类型安全。
- C++是强类型语言,
- 关键代码示例:
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector;
myVector.push_back(5); // 正确,插入int类型数据
// myVector.push_back("hello"); // 错误,"hello"是const char*类型,编译器会报错
return 0;
}