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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:55:17+00:00 2026-05-25T17:55:17+00:00

I have a string std::string s = Stack Overflow; That I need to copy

  • 0

I have a string

std::string s = "Stack Overflow";

That I need to copy into a vector.
This is how I am doing it

std::vector<char> v;
v.reserve(s.length()+1);
for(std::string::const_iterator it = s.begin(); it != s.end(); ++it)
{
    v.push_back( *it );
}
v.push_back( '\0' );

But I hear range operation are more efficient. So I am thinking something like this

std::vector<char> v( s.begin(), s.end());
v.push_back('\0');

But is this better in this case? What about the potential re-allocation when inserting ‘\0’?
Another approach I am thinking is this

std::vector<char> v(s.length()+1);
std::strcpy(&v[0],s.c_str());

Perhaps fast but potentially unsafe?
EDIT
Has to be a null terminated string that can be used ( read/write ) inside a C function

  • 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-25T17:55:18+00:00Added an answer on May 25, 2026 at 5:55 pm

    If you really need a vector (e.g. because your C function modifies the string content), then the following should give you what you want, in one line:

    std::vector<char> v(s.c_str(), s.c_str() + s.length() + 1);
    

    Since c_str() returns a null-terminated string, you can just copy it whole into the vector.

    However, I’m not actually sure how optimised this constructor is. I do know that std::copy is as optimised as it gets, so perhaps (measure!) the following is faster:

    std::vector<char> v(s.length() + 1);
    std::copy(s.c_str(), s.c_str() + s.length() + 1, v.begin());
    

    If the C function doesn’t modify the string, just pass c_str() directly, and cast away const-ness. This is safe, as long as the C function only reads from the string.

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

Sidebar

Related Questions

I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have some code that looks like: static const std::string and( AND ); This
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
I have unsigned char* , want to convert it to std::string . Can you
I have: class Foo { int a; int b; std::string s; char d; };
I have a member function like setResult(const std::string &s) { this->m_result = s; }
Basically I have a function that roughly looks like this and I need to
When using CDT I would like to have std::string show up in the 'variable'
I have the following code : std::string Utils::get() { std::string result; result.append(1, 'x'); result.append(1,

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.