How to iterate over a vector of many stucts
I have many nodes, done data is stored in a struct like this:
Code:
struct BriteNodeInfo
{
int nodeId;
double xCoordinate;
double yCoordinate;
int inDegree;
int outDegree;
int asId;
std::string type;
};
Each node instance is stored in a vector like this:
Code:
typedef std::vector<BriteNodeInfo> BriteNodeInfoList;
BriteNodeInfoList m_briteNodeInfoList;
Problem: How do I wrire a function that write in a .txt file my node data like this:
nodeId0 yCoordinate0 xCoordinate0
nodeId1 yCoordinate1 xCoordinate1
nodeId2 yCoordinate2 xCoordinate2
nodeId3 yCoordinate3 xCoordinate3
etc...
I have tried but my iteration syntax is not good enough. Here is my function, please help:
Code:
void SaveNodeData (std::string fname)
{
ofstream os(fname.c_str(), ios::trunc);
vector<BriteNodeInfo> BriteNodeInfoList;
BriteNodeInfoList m_briteNodeInfoList;
for (BriteNodeInfoList::Iterator i = m_briteNodeInfoList.Begin(); i != m_briteNodeInfoList.End(); ++i)
{
os << BriteNodeInfo[i].nodeId "\t" << "\t" << BriteNodeInfo[i].yCoordinate << "\t"BriteNodeInfo[i].xCoordinate<< "\n";
}
os << "\n";
}
Re: How to iterate over a vector of many stucts
What version of c++ are you using (c++98, c++11 or later)??
Can you use range-based for loops?
Consider for c++11 (not tried)
Code:
void SaveNodeData(const std::string& fname, const BriteNodeInfoList& bnil)
{
ofstream os(fname, ios::trunc);
if (!os.is_open()) {
cout << "Cannot open file " << fname << endl;
return;
}
for (const auto& bv : bnil)
os << bv.nodeId << '\t' << bv.yCoordinate << '\t' << bv.xCoordinate << endl;
os.close();
}
You need to pass the vector that contains the data you want to extract and store.
Re: How to iterate over a vector of many stucts
Quote:
I have tried but my iteration syntax is not good enough.
You don't have to use iterators to iterate over a vector. You could also do it the old-fashioned way like,
Code:
for (int i=0; i < int(m_briteNodeInfoList.size()); ++i)
{
os << BriteNodeInfo[i].nodeId << "\t" << BriteNodeInfo[i].yCoordinate; << "\t" << BriteNodeInfo[i].xCoordinate << "\n";
}
The size() function returns the number of elements in the vector. (it returns an unsigned int so it's cast to int to avoid a compiler warning.)
I'm mentioning this possibility to demonstrate that an std::vector can be treated very much like a static array. (the C++ standard even guarantees that the elements are laid out continuously in memory). But I recommend you use the range-based for-loop that 2kaud suggests. It uses iterators behind the scene but you don't have to deal with them explicitly which is a blessing (*).
---
(*) Because iterators really are pointers in disguise and thus inherently unsafe. Therefore many in the C++ community are eagerly awaiting the Range concept that will further push iterators into the background. One first step in this trend already available is the range-based for-loop.
Re: How to iterate over a vector of many stucts
If you want to stick to the iterator approach:
Code:
for (BriteNodeInfoList::iterator it = m_briteNodeInfoList.begin(), endIt = m_briteNodeInfoList.End(); it != endIt; ++it)
{
os << it->nodeId << "\t" << "\t" << it->yCoordinate << "\t" << it->xCoordinate << "\n";
}
and better with "auto"
Code:
for (auto it = m_briteNodeInfoList.begin(), endIt = m_briteNodeInfoList.End(); it != endIt; ++it)