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 1109213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:11:53+00:00 2026-05-17T02:11:53+00:00

I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The

  • 0

I wrote a small sparse matrix class with the member:

std::map<int,std::map<int,double> > sm;

The method below is the function i use to access the elements of a matrix, if not possible through an iterator:

double matrix::operator()(int r,int c) const
{
    std::map<int,std::map<int,double> >::const_iterator i = sm.find(r);
    if(i==sm.end()) { return 0.0; }
    std::map<int,double>::const_iterator j = i->second.find(c);
    if(j==i->second.end()) { return 0.0; }
    return j->second;
}

Still this function needs to get called very often. Has somebody an idea how to improve this function? Thanks ahead.

  • 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-05-17T02:11:53+00:00Added an answer on May 17, 2026 at 2:11 am

    If you are to write your own code rather than using a library, then this change may improve performance dramatically:

    std::map<std::pair<int,int>, double> sm;
    

    To increase farther you can go to hashed containers:

    std::unordered_map<std::pair<int,int>, double> sm;
    

    (use tr1, boost or C++0x)

    EDIT: in the first case you can iterate over row like this:

    for(auto& cell : make_pair(
        sm.lower_bound(make_pair(row, 0)), sm.lower_bound(make_pair(row+1, 0))))
    { ... }
    

    I think that you can do the above by one call to equal_range with an appropriate functor if you use Boost.MultiIndex.

    over everything:

    for(auto& cell : sm)
    { ... }
    

    to iterate over a column you need to search for every cell separately. Note that your datastructure doesn’t provide this operation either.

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

Sidebar

Related Questions

I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote a small coordinate class to handle both int and float coordinates. template
I wrote a small program using a custom indexOf function but wanted to dismiss
I wrote small service class and I want implement something like that : service
I wrote a small function to convert MB to Bytes, however, there seems to
I wrote a small test program with a sample class containing also self-defined constructor,
I wrote a small queue class in C++, on Windows. Just to test, I'm
I wrote a small Hello World app. #include <stdio.h> int main(int argc, const char
I wrote an small script to use split() as this, use strict; use warnings;
I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool

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.