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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:53:35+00:00 2026-06-17T12:53:35+00:00

In Java I can do List<String> data = new ArrayList<String>(); data.add(my name); How would

  • 0

In Java I can do

List<String> data = new ArrayList<String>();
data.add("my name");

How would I do the same in C++?

  • 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-17T12:53:36+00:00Added an answer on June 17, 2026 at 12:53 pm

    Use std::vector and std::string:

    #include <vector>  //for std::vector
    #include <string>  //for std::string
    
    std::vector<std::string> data;
    data.push_back("my name");
    

    Note that in C++, you don’t need to use new everytime you create an object. The object data is default initialized by calling the default constructor of std::vector. So the above code is fine.

    In C++, the moto is : Avoid new as much as possible.

    If you know the size already at compile time and the array doesn’t need to grow, then you can use std::array:

    #include <array> //for std::array
    
    std::array<std::string, N> data; //N is compile-time constant
    data[i] = "my name"; //for i >=0 and i < N
    

    Read the documentation for more details:

    • std::vector
    • std::string
    • std::array

    C++ Standard library has many containers. Depending on situation you have to choose one which best suits your purpose. It is not possible for me to talk about each of them. But here is the chart that helps a lot (source):

    enter image description here

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

Sidebar

Related Questions

In java can an instance variable and a method have the same name without
try { final List<String> ar = new ArrayList<String>(); final PRIvariable pri = new PRIvariable();
I have a method: private List<String> userCns = Collections.synchronizedList(new ArrayList<String>()); private List<String> recipients =
paramList = new ArrayList<String>(); paramList.add(line.split(,)); When I used this, it gives me the error:
How can I easily delete duplicates in a linked list in java?
I'm creating a Java application where the user can search through a list of
Which data type in Java can hold just the date and doesn't require a
Im trying to put arraylists into an arraylist. Adding data to the new arrays,
In Java can I create a URI for a file located locally in the
Is there anything simple Java can't do that can be done in a similar

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.