CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2017
    Posts
    1

    Exclamation 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";
     }
    Last edited by 2kaud; October 27th, 2017 at 01:11 PM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: How to iterate over a vector of many stucts

    Quote Originally Posted by 871020905@qq.com View Post
    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.
    Last edited by wolle; October 27th, 2017 at 01:18 PM.

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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)
    Last edited by 2kaud; October 27th, 2017 at 01:08 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured