在数据读入函数中将指针存储在vector中
Storing pointers in a vector in a data-read-in function
我有一个包含联赛,球队和球员的文本文件,看起来像这样:
League: some league
Team: some team
some players with name and strength
League: some other league
现在我用我的readin函数读取数据
#include "ReadIn.h"
#include "Player.h"
#include <deque>
#include <fstream>
#include <string>
#include <sstream>
#include <memory>
#include <iostream>
std::string zeile,word1, word2, sname, lname;
std::deque<Team*> Teamvector;
int str, a = 0, b = 0 , c = 0;
using namespace std;
void readin_fillVector(std::deque<Team> &v, std::deque<League> & w, std::vector<Player> u) {
ifstream fin("C:\Users\david\Documents\Visual Studio 2013\Projects\Anstoss2014\Datenbank\test.txt");
//Einlesen der Zeilen starten
while (getline(fin, zeile))
{
fin >> word1;
//find out if this line contains the team name or the players
if (word1 == "Liga:"){
getline(fin, word2);
//deleting the empty space in front of the team name
word2.erase(0, 1);
w.push_back(League(word2, c));
c++;
}
else if (word1 == "Verein:"){
getline(fin, word2);
//deleting the empty space in front of the team name
word2.erase(0, 1);
v.push_back(Team(word2, a));
//League gets the new member ( the team which was read in the line before)
w.back().AddTeam(&v.back());
//Team gets an pointer to the league it plays in
v.back().SetLeague(&w.back());
a++;
}
else{
fin >> lname >> str;
Player* player = new Player();
player->setinformation_(b, word1, lname, str, &v.back());
u.push_back(*player);
v.back().AddPlayer(player);
//for (size_t f = 0; f < v.back().GetPlayerList().size(); f++)
//{
// v.back().GetPlayerList()[f]->getinformation_();
//}
b++;
}
}
}
这是应该的,但我混淆了
这行Player* player = new Player();
我读了很多关于指针的文章,有人说这个由new()创建的播放器应该被删除。第一个问题是:1。
但是如果我在函数中这样做,则存储在球队playervector中的球员信息将丢失。
2。我有boost可访问,我应该使用boost::ptr_vector之类的东西吗?
3。如果没有,我还能做些什么来避免内存泄漏?
编辑:
另一种方法是
Player player;
player.setinformation_(b, word1, lname, str, &v.back());
u.push_back(player);
v.back().AddPlayer(&player);
但是当函数返回teamvector时,玩家没有存储更多信息。
首先,按值传递std::vector<Player>
。您可能打算通过引用传递它,否则您对它所做的任何更改都将丢失。
关于你的具体问题,你的"另一种方式"看起来几乎是正确的,但是你在teamvector中存储了一个指向本地player
变量的指针。当本地player
超出作用域时,该指针将悬空。你需要在播放器向量中存储一个指向播放器副本的指针,因为它拥有播放器。
然而,正如你所发现的,即使这也不会起作用,因为当玩家向量在push_back
期间被重新分配时,指向向量元素的所有指针都将失效。你可以提前在玩家向量中设置reserve
所需的所有空间,以确保不会发生重新分配,但你可能不知道reserve
有多少空间。您可以使用不同的STL容器,如std::list
,它承诺指针不会无效。但是如果你不想妥协你选择的容器,我建议你在堆上创建你的播放器对象,并使用智能指针来管理内存,无论是std::shared_ptr
还是std::unique_ptr
。所以函数原型看起来像:
in_fillVector(std::deque<Team> &v, std::deque<League> & w, std::vector<std::unique_ptr<Player>> &u)
和添加球员看起来像:
auto player = std::unique_ptr<Player>(new Player());
player->setinformation_(b, word1, lname, str, &v.back());
v.back().AddPlayer(player.get());
u.push_back(std::move(player));
相关文章:
- 将一个类的方法指针存储到另一个类中
- 将字符指针存储到容器中
- 用指向文件的指针存储数据结构
- C++17:unique_ptr<char[]> 和 shared_ptr<char[] 之间的指针存储差异>
- 将指针存储到矢量时内存泄漏
- 指针存储字符串?
- 将指针存储到SQLITE3数据库中的类别抛出segfault
- 指向本地变量的指针存储在此变量范围之外
- 将模板化对象指针存储在矢量中并通过基类指针进行访问
- 如何将指针存储在实例变量中,该指针被声明为指向基类的指针
- 将指针或一系列指针存储在动态阵列中
- 当对象的指针存储在矢量中时,如何访问对象中的方法?
- 将从方法返回的原始指针存储到智能指针中
- 将指向参数个数可变的函数的指针存储在结构对象中
- 将指向const的指针存储到structs的向量中
- 一系列指针存储地址并以后打印
- 将指针存储在std :: unordered_map中的不同类型的类中
- 超级级别的方法称为super具有sub的str attr 实例在循环 实例中创建的方法是作为super的指针存储在向
- 使用字符指针存储整数
- 我们是否应该将指向类实例的智能指针存储在大型 std::vector 中以获得更好的性能?