I am trying to use sort defined in algorithm in c++. I want to use it in vector string ( vector<string> vec) can we use it i vector string.

here is my code

``````#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
vector<string> vec;
vector<string> ::iterator it;
string s;
char b[5];

char a[5] = {'t','a','n','k','a'};

int l=strlen(a);
s = a;
vec.push_back(b);
for(int k=0;k<3;k++){
for(int i=0;i<5;i++){
if(i==0)
b[i] = a[4];
else
b[i] = a[i-1];
}
cout<<endl;
cout<<a;
for(int j=0;j<5;j++)
a[j] = b[j];

for(int j=0;j<5;j++)
s[j] = b[j];

vec.push_back(s);
}
vec.sort(vec.begin(),vec.end());
for(it = vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
cout<<s<<endl;

return 0;
}``````

i wanted to generate different combination of 'tanka'

2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by TSharma

line 16: variable a is NOT a string but just an array of characters because it is not null terminated. So string functions such as strlen() (line 18) and cout (line 29)will not work with it. To use strlen() you should have declared it like this: `char a[] = "tanka";` array b also needs to be increased by 1 to hold the null terminator character and it should be declared like this: `char b[6] = {0};` line 20: array b doesn't contain anything at that point so why are you adding it to the vector? And since b has not been initialized to anything it will cause the vector a lot of problems, possibly even crashing your program.

thank you i got my answer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.