Category:1972 births
Category:Living people
Category:English-language singers from Pakistan
Category:English songwriters
Category:Pakistani emigrants to the United Kingdom
Category:Naturalised citizens of the United Kingdom
Category:21st-century British singersQ:
Memory implications of reading and writing to std::vector
If I have a class representing a set of nodes, each of which has a pointer to its parent node. And I maintain a std::vector of these objects in an std::map of their parent node pointers. What are the memory implications if I have a function where I read and write the node pointer into the std::vector of objects and then insert it into the std::map of parent node pointers? I don't need to delete it later, so it can be assumed that all the references to the object are valid. Is it OK to just do:
void ReadNodeFromFile(const std::string &filename)
{
std::ifstream ifile(filename);
std::string input;
std::getline(ifile, input);
auto &n = input;
std::vector vec;
vec.push_back(n);
_parentMap.insert(std::make_pair(n, _parentMap.find(n->getParentNode()->getId())));
}
Or is there a better way to do this?
A:
To avoid copy, you could use std::copy and std::move
// using `std::copy`
std::vector vec;
std::ifstream ifile(filename);
if(!ifile)
{
//...
}
else
{
std::string input;
std::getline(ifile, input);
auto &n = input;
std::vector vec(std::move(input));
_parentMap.insert(std::make_pair(n, _parentMap.find(n->getParentNode()->getId())));
}
// using `std::move`
std::ifstream ifile(filename);
if(!ifile)
{
// ac619d1d87
Related links:
Comments