gpt4 book ai didi

c++ - 在 C++ 中,自定义数据结构的函数返回 vector 引发段错误

转载 作者:搜寻专家 更新时间:2023-10-30 23:52:00 26 4
gpt4 key购买 nike

我的程序可以编译,问题出在运行时。

我有以下 C++ 程序:

#include <iostream>
#include <vector>
#include <string>

struct SpotifyRelease {
std::string release_date, id, href, name;
};


SpotifyRelease get_release(const std::string id, const std::string href, const std::string name, const std::string release_date) {
SpotifyRelease sr;
sr.id = id;
sr.href = href;
sr.name = name;
sr.release_date = release_date;
return sr;
}

std::vector<SpotifyRelease> spotify_get_latest_releases() {
std::vector<SpotifyRelease> v1;
std::vector<SpotifyRelease>::iterator it;
v1.insert(it, get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"));
v1.insert(it, get_release("3HNnxK7NgLXbDoxRZxNWiR", "https://api.spotify.com/v1/albums/3HNnxK7NgLXbDoxRZxNWiR", "Kamikaze", "2018-08-31"));
v1.insert(it, get_release("1Xf5RZ7zQjF2WEkk7KpZVi", "https://api.spotify.com/v1/albums/1Xf5RZ7zQjF2WEkk7KpZVi", "Superhero", "2018-08-30"));
v1.insert(it, get_release("317hvEJEf6RTxPT4m2NiRb", "https://api.spotify.com/v1/albums/317hvEJEf6RTxPT4m2NiRb", "4Freedom", "2018-08-30"));
v1.insert(it, get_release("6ggnUqpjfkDULwX0dr164j", "https://api.spotify.com/v1/albums/6ggnUqpjfkDULwX0dr164j", "XTCY", "2018-08-30"));
v1.insert(it, get_release("6ST7naJFCe9iBeOleU5Ccu", "https://api.spotify.com/v1/albums/6ST7naJFCe9iBeOleU5Ccu", "Michael Jackson x Mark Ronson: Diamonds are Invincible", "2018-08-29"));
v1.insert(it, get_release("6OX3blc5vb3huGk2PTM0Kk", "https://api.spotify.com/v1/albums/6OX3blc5vb3huGk2PTM0Kk", "The Dark Side / Something Human / Thought Contagion / Dig Down", "2018-08-30"));
v1.insert(it, get_release("4o8Y3L9WQvk3L3u9M0gngq", "https://api.spotify.com/v1/albums/4o8Y3L9WQvk3L3u9M0gngq", "Don't Leave Me Alone (feat. Anne-Marie) [Oliver Heldens Remix]", "2018-08-31"));
v1.insert(it, get_release("0LOJHhz9ybWLVMLucHYtCe", "https://api.spotify.com/v1/albums/0LOJHhz9ybWLVMLucHYtCe", "REMEDY", "2018-08-31"));
v1.insert(it, get_release("1d1MKgYRqFUs8Mner4uF4y", "https://api.spotify.com/v1/albums/1d1MKgYRqFUs8Mner4uF4y", "Hola", "2018-08-31"));
v1.insert(it, get_release("3qX5J1RkaFxSXEqRc8Qzmo", "https://api.spotify.com/v1/albums/3qX5J1RkaFxSXEqRc8Qzmo", "Amigos Con Derechos", "2018-08-31"));
v1.insert(it, get_release("3OLSEsIMEgtcn1U9s03qRR", "https://api.spotify.com/v1/albums/3OLSEsIMEgtcn1U9s03qRR", "BEBE", "2018-08-31"));
v1.insert(it, get_release("2qIbHfs93z6eDwGoAdCkXA", "https://api.spotify.com/v1/albums/2qIbHfs93z6eDwGoAdCkXA", "8 Letters", "2018-08-31"));
v1.insert(it, get_release("3LBRCisCaxgQjb5nwSMPgU", "https://api.spotify.com/v1/albums/3LBRCisCaxgQjb5nwSMPgU", "Big Red Machine", "2018-08-31"));
v1.insert(it, get_release("29KdLwIDFZ8DSSgxOQ8kLa", "https://api.spotify.com/v1/albums/29KdLwIDFZ8DSSgxOQ8kLa", "Love Wins", "2018-08-31"));
v1.insert(it, get_release("1aG5r6TZdMVUeNGY6Lf4YP", "https://api.spotify.com/v1/albums/1aG5r6TZdMVUeNGY6Lf4YP", "Never Comin Down", "2018-08-31"));
v1.insert(it, get_release("0AykQgBtKmWJkQvomIKYSF", "https://api.spotify.com/v1/albums/0AykQgBtKmWJkQvomIKYSF", "Rotation 112th (feat. Rich The Kid) [Remix]", "2018-08-31"));
v1.insert(it, get_release("28cah7y6l6TUl5G7aP5pZf", "https://api.spotify.com/v1/albums/28cah7y6l6TUl5G7aP5pZf", "Serious", "2018-08-30"));
v1.insert(it, get_release("1xKEX6KhO9pRM85WT7aOel", "https://api.spotify.com/v1/albums/1xKEX6KhO9pRM85WT7aOel", "Weed Garden", "2018-08-31"));
v1.insert(it, get_release("5eOlKFDAN9dXwYycRqEmnP", "https://api.spotify.com/v1/albums/5eOlKFDAN9dXwYycRqEmnP", "Flow State", "2018-08-31"));
return v1;
}

int main() {
std::vector<SpotifyRelease> latest_releases;
SpotifyRelease release;
latest_releases = spotify_get_latest_releases();
return 0;
}

问题是当我尝试运行该程序时,我的程序在 int main() 的第二行退出并显示代码 11

我不太确定为什么,因为程序对我来说看起来还不错。

我们将不胜感激在正确方向上的任何帮助。

最佳答案

您将未初始化的迭代器传递给 insert。这是未定义的行为。

您的意思可能是 auto it = begin(v1);,但这仍然行不通,因为您没有在任何地方重新分配 it,并且 std::vector::insert 可能会使 vector 元素的所有现有迭代器无效。

我想您真的不想使用 insert 。只需使用 push_back:

v1.push_back(get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"));
// ...

关于c++ - 在 C++ 中,自定义数据结构的函数返回 vector 引发段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com