-3

I want to increase the size of the array of string after declaring it once, how can it be done. I need to increase the size in the following code..

#include<iostream>
using namespace std;
#include<string>
int main()
{
    int n;
    string A[] =
    { "vaibhav", "vinayak", "alok", "aman" };
    int a = sizeof(A) / sizeof(A[0]);
    cout << "The size is " << a << endl;
    for (int i = 0; i < a; i++)
    {
        cout << A[i] << endl;
    }
    cout << "Enter the number of elements you want to add to the string"
            << endl;
    cin >> n;
    cout << "ok now enter the strings" << endl;
    for (int i = a; i < n + a; i++)
    {
        cin >> A[i];
    }
    a = a + n;
    A.resize(a); // THIS KIND OF THING
    for (int i = 0; i < a; i++)
    {
        cout << A[i] << endl;
    }
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
vaibnak
  • 467
  • 3
  • 13

4 Answers4

3

Plain and simple: you cannot.

You can get a larger array, copy all your stuff over and use that instead. But why do all that, when there is a perfectly good class already there, doing it all for you: std::vector.

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> A = {"vaibhav", "vinayak", "alok", "aman"};

    std::cout << "The size is " << A.size() << std::endl;

    for(string s : A) 
    {
        std::cout << s << std::endl;
    }

    // want to enter more?
    sd::string more;
    std::cin >> more;
    A.push_back(more);

    std::cout << "The size is " << A.size() << std::endl;
    for(string s : A)
    {
        std::cout << s << std::endl;
    }

    return 0;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I was just trying to think of the ways I can do it without using vectors, thanks for the confirmation – vaibnak Dec 15 '17 at 17:05
  • 2
    @vaibnak: `std::vector` is the standard, idiomatic, default way to do this. There isn't much of a reason for finding ways not to use it. – Christian Hackl Dec 15 '17 at 17:07
  • 1
    @vaibnak There are at least a million ways to do this without using `std::vector`. There are only about 5 good reasons to prefer one of those methods over using `std::vector`, and I'm going to suggest that whatever your reasons are, they're not one of those 5 reasons. – Xirema Dec 15 '17 at 17:11
  • 1
    the most common reason for not using vector is 'we are not allowed to use vector cos the instructor says so' – pm100 Dec 15 '17 at 17:48
3

Convert your code over to use std::vector and this problem becomes much easier to solve.

#include<iostream>
#include<string>
#include<vector>

int main(){
    int n;
    std::vector<std::string> A = {"vaibhav", "vinayak", "alok", "aman"};
    int a = A.size();
    std::cout << "The size is " << a << std::endl;
    //Prefer Range-For when just iterating over all elements
    for(std::string const& str : A){
        std::cout << str << std::endl;
    }
    std::cout << "Enter the number of elements you want to add to the string" << std::endl;
    std::cin >> n;
    std::cout << "ok now enter the strings" << std::endl;
    for(int i = 0; i < n; i++ ) {
        //emplace_back automatically resizes the container when called.
        A.emplace_back();
        std::cin >> A.back();
        //If you're using C++17, you can replace those two lines with just this:
        //std::cin >> A.emplace_back();
    }
    for(std::string const& str : A){
        std::cout << str << std::endl;
    }
    return 0;
}

Also, don't use using namespace std;, since it leads to expensive to fix bugs and makes your code harder to read for other C++ programmers.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Xirema
  • 19,889
  • 4
  • 32
  • 68
2

I want to increase the size of the array of string after declaring it once, how can it be done.

It cannot be done. Use std::vector if the element count isn't known at compile time or can change dynamically. It even has a resize member function named exactly like the one in your code.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
1

You cannot increase the size of a Raw Array, you could use an std::vecto<std::string> as this type of array can grow at runtime.

However, you could also create a class that will store an array of string and create your own implementation to resize the raw array. Which would be creating a bigger array and copying all the other values over, then setting the class array to the new array (or just return it)

peti446
  • 330
  • 1
  • 4
  • 12
  • 1
    *"However, you could also create a class that will store an array of string and create your own implementation to resize the raw array."* - And why would you ever want to do such a thing instead of wrapping a `std::vector`? – Christian Hackl Dec 15 '17 at 17:04
  • True point, it is kind of pointless doing so having a vector, however, it's a workaround if you want just to use a raw array. – peti446 Dec 15 '17 at 17:07