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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:25:21+00:00 2026-06-15T22:25:21+00:00

How to copy span of chars from char array to vector ? Starting index

  • 0

How to copy span of chars from char array to vector ? Starting index is always 0 and goes to some passed value x. I have char* buffer on heap ( size is 32*1024) and I am using that buffer to receive messages and set received message size in variable x. How to copy from 0th to xth char to vector<char> mainBuffer ?
( I can simply iterate but it looks inefficient way if message is long. Another way is like below but then I always in every pass create new vector)

char* buffer = new char[32*1024];
int x;
std::vector<std::vector<char> > mainBuffer;
//:loop
// here is some code where I recive message in buffer and set x
mainBuffer.push_back(std::vector<char>(buffer,buffer+x));
//:end loop

Does anyone know more efficient and elegant way to do this ?

  • 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-06-15T22:25:23+00:00Added an answer on June 15, 2026 at 10:25 pm
    mainBuffer.push_back(std::vector<char>(buffer,buffer+x));
    

    Does anyone know more efficient and elegant way to do this ?

    With C++11, do:

    mainBuffer.emplace_back( buffer,buffer+x );
    

    This forwards the parameters and constructs the object directly inside mainBuffer.

    Edit: With C++ (old), use std::swap:

    mainBuffer.push_back( std::vector<char>() );
    mainBuffer.back().swap( std::vector<char>(buffer, buffer+x) );
    

    Edit 2: If you can, call reserve on mainBuffer before using push_back on it.

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

Sidebar

Related Questions

i have such code (copy paste from wiki). Its multiplication of those big numbers
I try to copy an onClick function from an image to an span object.
We run some large directories where users often copy/paste content from word documents etc
I have users that require to copy and paste from word (or excel) into
I've created a javascript function that will take a hidden span, copy the text
I copy-pasted from MSDN this code: using System.Security.Cryptography; byte[] buffer = enc.GetBytes(text); SHA1CryptoServiceProvider cryptoTransformSHA1
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy is aimed at fast
I copy and paste code from this URL for creating and reading/writing a proc
I want to copy a selection of lines from a file to another file
I have a list, where SPANs contains a number. I want to copy this

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.