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

  • Home
  • SEARCH
  • 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 6752587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:44+00:00 2026-05-26T13:03:44+00:00

I am trying to write some simple Rcpp code examples. This is remarkably easy

  • 0

I am trying to write some simple Rcpp code examples. This is remarkably easy with the Rcpp and inline packages.

But I am stumped on how to test whether two character elements for equality. The following example compares the first elements of two character vectors. But I can’t get it to compile.

What is the trick?

library(Rcpp)
library(inline)

cCode <- '
    Rcpp::CharacterVector cx(x);
    Rcpp::CharacterVector cy(y);
    Rcpp::LogicalVector r(1);
    r[0] = (cx[0] == cy[0]);
    return(r);
    '

cCharCompare <- cxxfunction(signature(x="character", y="character"), 
                            plugin="Rcpp", body=cCode)
cCharCompare("a", "b")

—

The comparison using == works perfectly fine if one of the two elements is a constant. The following code compiles and gives expected results:

cCode <- '
    Rcpp::CharacterVector cx(x);
    Rcpp::LogicalVector r(1);
    r[0] = (cx[0] == "a");
    return(r);
    '

cCharCompareA <- cxxfunction(signature(x="character"), plugin="Rcpp", body=cCode)

cCharCompareA("a")
[1] TRUE

cCharCompareA("b")
[1] FALSE
  • 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-26T13:03:45+00:00Added an answer on May 26, 2026 at 1:03 pm

    Very nice (technical) answer by @kohske, but here is something more C++-ish: just compare strings!

    library(inline)      ## implies library(Rcpp) when we use the plugin
    
    cCode <- '
        std::string cx = Rcpp::as<std::string>(x);
        std::string cy = Rcpp::as<std::string>(y);
        bool res = (cx == cy);
        return(Rcpp::wrap(res));
        '
    
    cCharCompare <- cxxfunction(signature(x="character", y="character"),
                                plugin="Rcpp", body=cCode)
    cCharCompare("a", "b")
    

    If you really want to compare just the first character of the strings, then you can go from x to x.c_str() and either index its initial element, or just dereference the pointer to the first char.

    A more R-ish answer could maybe sweep over actual vectors of strings…

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

Sidebar

Related Questions

I'm trying to write a simple integration test, but having some trouble with Domain
I'm trying to write some simple test code as a demonstration of hooking the
I'm trying to write some simple code that will return the directory for the
I'm trying to write some simple C# code that will select some data from
I'm trying to write some code to test out the Cartesian product of a
I'm trying to write some simple chat client in javascript, but I need a
I'm trying to write some simple code to resize an image, and I am
I'm trying to write simple proxy server for some purpose. In it I use
I'm trying to write a simple AppleScript to do some text manipulation on the
I'm trying to write some C# code to get to a specific folder in

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.