0
    std::string mstring[5];
        mstring[0] = "veena";
        mstring[1] = "guitar";
        mstring[2] = "sitar";
        mstring[3] = "sarod";
        mstring[4] = "mandolin";

I want to assign the array like above. I don't want to do it at initialization but assign later. Is there a way to combine 5 statements into one.

8
  • Dupe: Can one (re)set all the values of an array in one line (after it has been initialized)? Commented Jul 16, 2022 at 11:04
  • 2
    @AnoopRana C++ has no array literals, and the answer there definitely doesn't apply. Commented Jul 16, 2022 at 11:16
  • 1
    @lorem1213 I don't want to do it at initialization but assign later. -- A question -- why aren't you declaring your variables close(r) to where you will actually use them? What's the reason for declaring the array so far away from it's actual use point? One advantage that C++ gives you over C is that you can declare variables at the point of usage, and not at the top of a { } block. Commented Jul 16, 2022 at 11:20
  • 1
    To answer the question as asked, no there is not a way. Raw arrays (as distinct from std:array in C++, which has different capabilities) cannot be assigned in a single statement (except by techniques to do multiple assignment in one statement, for example separated by comma operator, but that's apparently not what is sought here). There's still the valid question of WHY you want to do that, since it is completely unnecessary - by DESIGN - in C++ to define any variable (or array) before it can be initialised. Commented Jul 16, 2022 at 11:24
  • 1
    @lorem1213 { ...some code...; std::string mystring[] = {"a","b","c","d","e"}; ...code using mystring...} -- Code organized this way makes your question moot. Also recommended C++ coding style is to declare variables close to where they will be used. Commented Jul 16, 2022 at 11:32

3 Answers 3

1

You can do that by using std::array<std::string, 5> instead of the raw array.

For example

#include <iostream>
#include <string>
#include <array>

int main()
{
    std::array<std::string, 5> mstring;
    
    mstring = { "veena", "guitar", "sitar", "sarod", "mandolin" };

    for ( const auto &s : mstring )
    {
        std::cout << s << ' ';
    }
    std::cout << '\n';
}

The program output is

veena guitar sitar sarod mandolin 

Another approach when a raw array is used is to use std::initializer_list in range-based for loop. For example

#include <iostream>
#include <string>

int main()
{
    std::string mstring[5];
    
    size_t i = 0;

    for ( auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
    {
        mstring[i++] = s;
    }

    for ( const auto &s : mstring )
    {
        std::cout << s << ' ';
    }
    std::cout << '\n';
}

The program output is the same as shown above

veena guitar sitar sarod mandolin 

If your compiler supports C++ 20 then instead of these statements

size_t i = 0;

for ( auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
{
    mstring[i++] = s;
}

you can use just one range-based for loop

for ( size_t i = 0; auto s : { "veena", "guitar", "sitar", "sarod", "mandolin" } )
{
    mstring[i++] = s;
}
Sign up to request clarification or add additional context in comments.

8 Comments

@AnoopRana OP can not even know about std::array. So this information is useful for him.
If so, you could've edited the dupe list instead of reopening the question. There are plenty of dupes covering how to assing to std::array or how to use std::array and their advantages over built in array.
@AnoopRana It is not interesting to me to find duplicates.
@AnoopRana It is the first time when I answer to such a question. If I will search duplicates I will forget C++ myself.:) I need a practice.:)
@AnoopRana He is a beginner. You should take that into account. Moreover I showed how it can be done with a raw array. This can not be done in C. You should upvote the question of the beginner.
|
1

This is a two-liner, but I think one-lining is not possible with native arrays (at least to my best knowledge).

  1. Initialize a temporary array with the desired values
  2. swap pointers with the original array

In that way you can use the one-line initialization for arrays, but still can manipulate the original array before setting the values.

#include <string>
#include <iostream>

int main(){
    
  std::string mystring[5];
  std::string tmp[5] = {"veena","guitar","sitar", "sarod", "mandolin"};
  std::swap(mystring, tmp);
  for(auto& s : mystring) std::cout << s << std::endl;
  return 0;
}

Comments

-1

You can use comma operator to combine multiple expressions into one statement.

mstring[0] = "veena",
mstring[1] = "guitar",
mstring[2] = "sitar",
mstring[3] = "sarod",
mstring[4] = "mandolin";

2 Comments

+1 for Best Abuse of The Rules. Now it is one statement, as required, (but unfortunately still 5 lines).
@BoP Remove the newline characters to make it single line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.