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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:59:19+00:00 2026-05-25T14:59:19+00:00

I have three questions based on the following code fragments I have a list

  • 0

I have three questions based on the following code fragments
I have a list of strings. It just happens to be a vector but could potentially be any source

vector<string> v1_names = boost::assign::list_of("Antigua and Barbuda")( "Brasil")( "Papua New Guinea")( "Togo");

The following is to store lengths of each name

vector<int> name_len;

the following is where I want to store the strings

std::vector<char> v2_names;

estimate memory required to copy names from v1_names

v2_names.reserve( v1_names.size()*20 + 4 );

Question: is this the best way to estimate storage? I fix the max len at 20 that is ok, then add space for null treminator
Now copy the names

for( std::vector<std::string>::size_type i = 0; i < v1_names.size(); ++i)
{
    std::string val( v1_names[i] );
    name_len.push_back(val.length());
    for(std::string::iterator it = val.begin(); it != val.end(); ++it)
    {
        v2_names.push_back( *it );
    }
    v2_names.push_back('\0');
}

Question: is this the most efficient way to copy the elements from v1_name to v2_names?

Main Question: How do I iterate over v2_names and print the country names contained in v2_names

  • 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-25T14:59:20+00:00Added an answer on May 25, 2026 at 2:59 pm

    To estimate storage, you should probably measure the strings, rather than rely on a hard-coded constant 20. For example:

    size_t total = 0;
    for (std::vector<std::string>::iterator it = v1_names.begin(); it != v1_names.end(); ++it) {
        total += it->size() + 1;
    }
    

    The main inefficiency in your loop is probably that you take an extra copy of each string in turn: std::string val( v1_names[i] ); could instead be const std::string &val = v1_names[i];.

    To append each string, you can use the insert function:

    v2_names.insert(v2_names.end(), val.begin(), val.end());
    v2_names.push_back(0);
    

    This isn’t necessarily the most efficient, since there’s a certain amount of redundant checking of available space in the vector, but it shouldn’t be too bad and it’s simple. An alternative would be to size v2_names at the start rather than reserving space, and then copy data (with std::copy) rather than appending it. But either one of them might be faster, and it shouldn’t make a lot of difference.

    For the main question, if all you have is v2_names and you want to print the strings, you could do something like this:

    const char *p = &v2_names.front();
    while (p <= &v2_names.back()) {
        std::cout << p << "\n";
        p += strlen(p) + 1;
    }
    

    If you also have name_len:

    size_t offset = 0;
    for (std::vector<int>::iterator it = name_len.begin(); it != name_len.end(); ++it) {
        std::cout << &v2_names[offset] << "\n";
        offset += *it + 1;
    }
    

    Beware that the type of name_len is technically wrong – it’s not guaranteed that you can store a string length in an int. That said, even if int is smaller than size_t in a particular implementation, strings that big will still be pretty rare.

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

Sidebar

Related Questions

I have the following questions concerning code generation for Hibernate annotated DTO/DAO-s: Is it
I have three easy questions. Does anybody use QuickTest Pro for automated testing? Any
There have been questions with answers on how to write rubygems, but what should
There have been a couple of questions that sort of dealt with this but
Thanks to those who answered my last couple questions, I got the following code
In a web application that I have run across, I found the following code
The following code is based on the example given in the javadocs for java.util.zip.Deflater
I know there have been similar questions posted but I think the issue I'm
Code to replicate In my app I have the following code. var seed =
I know there have been questions in the past about SQL 2005 versus Lucene.NET

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.