Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:15:04+00:00 2026-06-13T03:15:04+00:00

I have a piece of code that I’m migrating from Fortran to C++, and

  • 0

I have a piece of code that I’m migrating from Fortran to C++, and I’d like to avoid some of the nested for loop structures I had to create in the original F77 code.

The problem is this: I have a vector of objects called nodes that each include a vector holding (among other important info) the indices of other node objects to which each is connected (a connection graph). Like this

struct Node {
    vector<int> conNode;
};
vector<Node> listOfNodes;
vector<int> nodeListA;    // a subset of nodes of interest stored as their vector indices

I need to look for nodes that nodes in nodeListA are connected to, but only if those nodes are also in nodeListA. Right now, my code looks something like this:

// Loop over the subset of node indices
for (int i=0; i<nodeListA.size(); i++) {
    // Loop over the nodes connected to the node i
    for (int j=0; j<listOfNodes[nodeListA[i]].conNode.size(); j++) {
        // Loop over the subset of node indices again
        for (int k=0; k<nodeListA.size(); k++) {
            // and determine if any of node i's connections are in the subset list
            if (nodeListA[k] == listOfNodes[nodeListA[i]].conNode[j]) {
               // do stuff here
            }
        }
    }
}

There HAS to be a much simpler way to do this. It seems like I’m making this way too complicated. How can I simplify this code, possibly using the standard algorithm library?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:15:05+00:00Added an answer on June 13, 2026 at 3:15 am

    If your variable should express a set of values, use std::set instead of std::vector. Then you’ll have

    typedef std::set<int> SetOfIndices;
    SetOfIndices setOfIndices; // instead of nodeListA
    for(SetOfIndices::const_iterator iter = setOfIndices.begin(); iter != setOfIndices.end(); ++iter)
    {
        Node const & node = listOfNodes[*iter];
        for (int j = 0; j < node.conNode.size(); ++j)
        {
            if (setOfIndices.find(node.conNode[j]) != setOfIndices.end())
            {
                // do stuff here
            }
        }
    }
    

    EDIT
    As Jerry Coffin suggests, std::set_intersection can be used in outer loop:

    struct Node {
        SetOfIndices conNode;
    }
    typedef std::set<int> SetOfIndices;
    SetOfIndices setOfIndices; // instead of nodeListA
    for(SetOfIndices::const_iterator iter = setOfIndices.begin(); iter != setOfIndices.end(); ++iter)
    {
        Node const & node = listOfNodes[*iter];
        std::vector<int> interestingNodes;
    
        std::set_intersection(setOfIndices.begin(), setOfIndices.end(),
                          node.conNode.begin(), node.conNode.end(),
                          std::back_inserter(interestingNodes));
    
        for (int j = 0; j < interestingNodes.size(); ++j)
        {
            // do stuff here
        }
    }
    

    ANOTHER EDIT
    About efficiency – it depends what is the dominant operation. The number of executions of part described as “do stuff here” will not vary. The difference is in time of traversing your collections:

    1. Your original code – nodeListA.size()^2 * [average conNode size]
    2. My first solution – nodeListA.size() * log(nodeListA.size()) * [average conNode size]
    3. After Jerry Coffin suggestion – nodeListA.size()^2 * [average number of interesting conNode elements]

    So it seems that set_intersection use doesn’t help in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If have a piece of code that gets some data from a sql database
I have a piece of code that's removing some unwanted lines from a text
I have a piece of code that takes several rows from a database and
I have a piece of code that encodes some kind of business data in
I have a piece of code that essentially looks like this: <div class=parent> <a
I have a piece of code that looks like this: public static T CreateSomething<T>(SomeType
I have a piece of code that I would like to set as part
I have some piece of code that behaves differently under Mac OSX and Linux
I have a piece of code that looks something like this (ClearImportTable and InsertPage
We have some piece of code that behaves differently in two .NET versions: *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.