在C++中,函数返回自定义数据结构向量抛出一个分段错误

ILi*_*cos 2 c++ vector

我的程序编译,问题是在运行时.

我在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;
}
Run Code Online (Sandbox Code Playgroud)

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

我不太确定为什么,因为程序看起来对我好.

任何正确方向的帮助将不胜感激.

Chr*_*ckl 5

你传递一个未初始化的迭代器insert.这是未定义的行为.

你可能意味着类似的东西auto it = begin(v1);,但是这仍然不起作用,因为你不会it在任何地方重新分配,并且std::vector::insert可能使所有现有的迭代器无效到向量的元素.

我想你insert不管怎么说都不想用.只需使用push_back:

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