How do i accept a delimiter as space or endline both in getline

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool lexiocompare(string a,string b)
{
    return a>b;
}
void generator(string a[],int m)
{  cin.get();
   for (int i = 0; i < m; i++)
   {
       getline(cin,a[i],' ');
   }
   
    sort(a,a+m,lexiocompare);
    for (int i = 0; i <m; i++)
    {
        cout<<a[i];
    }
    
}
int main(){
    int t,m;
    string a[100000];
    cin>>t>>m;
    while (t)
    {
        generator(a,m);
    t--;
    }
    
    
    return 0;
}

hi @sagar_aggarwal can you tell what exactly are you trying to do, so i’ll be able to help you better

1
4
54 546 548 60
this is my input and the required output is 6054854654
i using string to take the input and i am sorting it lexio-graphically to generate my ans in getline function at line 13 predefined delimiter is ‘\n’ thats why i am not able to pass all test cases help me how to solve this

@sagar_aggarwal you dont need to take input by getline, you can make an array of strings, and take input for n strings via cin only. Then you can sort the string array by custom comparator

will it be automatically terminated with null? can you please edit what are you saying in my code please

@sagar_aggarwal yes if you take input for a string datatype, you dont need to add null character manually.

void generator(string a[],int m)
{  
   for (int i = 0; i < m; i++)
   {
       cin>>a[i];
   }
   
    sort(a,a+m,lexiocompare);
    for (int i = 0; i <m; i++)
    {
        cout<<a[i];
    }
    
}

You mean this? still it is not passing all test cases

@sagar_aggarwal your compare function is not fully correct. By this logic, you will give wrong answer for cases like
1
2
60
6006

the correct answer is 606006 but your answer will be 600660
so you should actually compare a+b with b+a (the + operator will concatenate the strings)

none of the testcase is passing

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool lexiocompare(string a,string b)
{
    return a+b>b+a;
}
void generator(string a[],int m)
{  cin>>m;
    char ch;
   ch=cin.get();
   for (int i = 0; i < m; i++)
   {
       cin>>a[i];
   }
    sort(a,a+m,lexiocompare);
    for (int i = 0; i <m; i++)
    {
        cout<<a[i];
    }
    
}
int main(){
    int t,m;
    string a[100000];
    cin>>t;
    while (t)
    {
        generator(a,m);
    t--;
    }
    
    
    return 0;
}

@sagar_aggarwal answer of each test case should be in a separate line.
And take input for m inside the while loop.

Please mark the doubt in case of no further queries :slight_smile: