如何使用向量为字符串赋值
How to assign values to String using vector?
struct Books{
std::string title;
std::string author;
std::string des;
int book_id = 0;
char identifier;
} ;
// book 0
book[0].title = "Programming Fundamentals"; // subscript out of range
book[0].author, "Robert Hanks";
book[0].des, "Programming Basics";
book[0].book_id = 101;
book[0].identifier = 'P';
// Struct Object
std::vector <Books> book;
当我尝试编译上面的代码时,它会给我一个超出范围的下标错误。
我做错了什么吗?
谢谢。
您创建了一个空向量并尝试通过语句访问book[0]
其元素,这是不正确的。 在使用 book[0]
访问向量之前,您需要在向量中至少包含一个元素。
以在向量中至少包含一个元素。我在下面举一个例子来修复它。
// Struct Object
std::vector <Books> book(1);
// book 0
book[0].title = "Programming Fundamentals"; // subscript out of range
....
....
相关文章:
- 如何使用向量为字符串赋值
- 避免在 ctor 初始值设定项列表中的字符串赋值中使用双重调用函数
- 无法为字符串赋值(Eclipse CDT、MinGW、C++)
- 带有初始值设定项列表的字符串赋值
- R 值重载运算符字符串赋值错误
- 使用for循环的C字符串赋值
- 正在将std::string中的子字符串赋值为c样式字符串
- 字符串赋值中的代码转储
- 当我们可以使用=运算符为字符串赋值时,为什么要在赋值之前使用new运算符为字符串动态分配内存
- 析构函数忽略字符串赋值
- 如果右手边包含字符串文字的串联,则C++const std::字符串赋值错误
- 在c++中如何从另一个字符串赋值未初始化的字符串值
- 字符串赋值错误
- 蠢Q:将字符串赋值到字符数组中
- 如何将字符串赋值为十六进制
- 字符串赋值差异
- std::通过索引进行字符串赋值似乎不起作用
- 当常量字符串赋值给常量字符指针时会发生什么?
- 字符串赋值中的Coredump
- 这里面有什么错误?字符串赋值