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'

Recommended Answers

All 2 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.