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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:23:17+00:00 2026-05-27T20:23:17+00:00

I have a snippet of code like this: std::list<boost::shared_ptr<Point> > left, right; // …

  • 0

I have a snippet of code like this:

std::list<boost::shared_ptr<Point> > left, right;
// ... fill lists ...

// now, calculate the angle between (right[0], right[1]) and (right[0], left[0])
double alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) );

std::cout << alpha * 180 / M_PI << std::endl;

if(alpha < 0){
    // do something with the lists, like reversing them. Especially the beginning and end of the lists may change in some way, but "left" and "right" are not reassigned.
}

// calculate the new alpha
alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) );

Apart from the iterator increment magic which may not be too obvoius here without the comments, I’d like to define a function double alpha() to reduce the duplication. But because the use of this function is very specific, I’d like to make it a local function. Ideally like that:

int a, b;
int sum(){ return a + b; }
a = 5; b = 6;
int s = sum(); // s = 11
a = 3;
s = sum(); // s = 9 now

In languages like Python this would be perfectly ok, but how to do this in C++?

EDIT:

This is what I ended up with, special thanks to @wilx and the -std=c++0x compiler flag:

auto alpha = [&right, &left]() {

    // not 100% correct due to my usage of boost::shared_ptr, but to get the idea

    Point r_first = *(right.begin()); 
    Point l_first = *(left.begin());
    Point r_second = *(++right.begin());

    return angle(r_first, r_second, l_first);
};


if(alpha() < 0) // fix it

double new_alpha = alpha();
  • 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-27T20:23:18+00:00Added an answer on May 27, 2026 at 8:23 pm

    In this case I would suggest using an overload like angle2(std::list<Point> const &, etc.). Two simple arguments is better than what you have now.

    With C++11 you could use lambdas that catch their arguments by reference.

    If you cannot use C++11 and you do feel adventurous, try Boost.Phoenix (part of Boost.Spirit).

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

Sidebar

Related Questions

Say, I have a code snippet like this: public static void main(String[] args) {
I have a snippet of code that looks like this: double Δt = lastPollTime
I have a short snippet of C# code like this: HtmlGenericControl titleH3 = new
I have a code snippet that looks like this: [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
I have code snippet, which is like this... #import <sqlite3.h> @interface DatabaseClass : NSObject
I have a code snippet that goes like this: $('#content').css('padding-top', heightTwo + 44 +
I have a code snippet which connects to a MySQL database like this (not
I have a snippet of code like this: var profileLinks = new Array(); for
// Refer: http://en.wikipedia.org/wiki/Decimal I have a snippet like this in my java source code,
I have a snippet of code like this in a Django application def update_account(user_account,add_widget_count):

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.