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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:55:18+00:00 2026-05-31T15:55:18+00:00

I’ve recently been porting a Python application to C++, but am now at a

  • 0

I’ve recently been porting a Python application to C++, but am now at a loss as to how I can port a specific function. Here’s the corresponding Python code:

def foo(a, b): # Where `a' is a list of strings, as is `b'
    for x in a:
        if not x in b:
            return False

    return True

I wish to have a similar function:

bool
foo (char* a[], char* b[])
{
    // ...
}

What’s the easiest way to do this? I’ve tried working with the STL algorithms, but can’t seem to get them to work. For example, I currently have this (using the glib types):

gboolean
foo (gchar* a[], gchar* b[])
{
    gboolean result;

    std::sort (a, (a + (sizeof (a) / sizeof (*a))); // The second argument corresponds to the size of the array.
    std::sort (b, (b + (sizeof (b) / sizeof (*b)));

    result = std::includes (b, (b + (sizeof (b) / sizeof (*b))),
                            a, (a + (sizeof (a) / sizeof (*a))));

    return result;
}

I’m more than willing to use features of C++11.

  • 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-31T15:55:19+00:00Added an answer on May 31, 2026 at 3:55 pm

    Your first problem is related to the way arrays are (not) handled in C++. Arrays live a kind of very fragile shadow existence where, if you as much as look at them in a funny way, they are converted into pointers. Your function doesn’t take two pointers-to-arrays as you expect. It takes two pointers to pointers.

    In other words, you lose all information about the size of the arrays. sizeof(a) doesn’t give you the size of the array. It gives you the size of a pointer to a pointer.

    So you have two options: the quick and dirty ad-hoc solution is to pass the array sizes explicitly:

    gboolean foo (gchar** a, int a_size, gchar** b, int b_size)
    

    Alternatively, and much nicer, you can use vectors instead of arrays:

    gboolean foo (const std::vector<gchar*>& a, const std::vector<gchar*>& b)
    

    Vectors are dynamically sized arrays, and as such, they know their size. a.size() will give you the number of elements in a vector. But they also have two convenient member functions, begin() and end(), designed to work with the standard library algorithms.

    So, to sort a vector:

    std::sort(a.begin(), a.end());
    

    And likewise for std::includes.

    Your second problem is that you don’t operate on strings, but on char pointers. In other words, std::sort will sort by pointer address, rather than by string contents.

    Again, you have two options:

    If you insist on using char pointers instead of strings, you can specify a custom comparer for std::sort (using a lambda because you mentioned you were ok with them in a comment)

    std::sort(a.begin(), a.end(), [](gchar* lhs, gchar* rhs) { return strcmp(lhs, rhs) < 0; });
    

    Likewise, std::includes takes an optional fifth parameter used to compare elements. The same lambda could be used there.

    Alternatively, you simply use std::string instead of your char pointers. Then the default comparer works:

    gboolean
    foo (const std::vector<std::string>& a, const std::vector<std::string>& b)
    {
        gboolean result;
    
        std::sort (a.begin(), a.end());
        std::sort (b.begin(), b.end());
    
        result = std::includes (b.begin(), b.end(),
                                a.begin(), a.end());
    
        return result;
    }
    

    Simpler, cleaner and safer.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.