Click here to Skip to main content
15,178,226 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: A RichEdit without any borders Pin
Rozis29-Jun-10 5:03
MemberRozis29-Jun-10 5:03 
GeneralRe: A RichEdit without any borders Pin
Niklas L29-Jun-10 11:06
MemberNiklas L29-Jun-10 11:06 
AnswerRe: A RichEdit without any borders Pin
Xeqtr26-Jun-10 11:44
MemberXeqtr26-Jun-10 11:44 
GeneralRe: A RichEdit without any borders Pin
Rozis29-Jun-10 5:01
MemberRozis29-Jun-10 5:01 
Questionsorting alphabetically Pin
raju_shiva25-Jun-10 22:53
Memberraju_shiva25-Jun-10 22:53 
AnswerRe: sorting alphabetically Pin
Richard MacCutchan25-Jun-10 23:18
mveRichard MacCutchan25-Jun-10 23:18 
AnswerRe: sorting alphabetically Pin
Shivanand Gupta26-Jun-10 3:40
MemberShivanand Gupta26-Jun-10 3:40 
AnswerRe: sorting alphabetically [modified] Pin
Aescleal26-Jun-10 5:05
MemberAescleal26-Jun-10 5:05 
If you only want the strings for one purpose then it's probably more efficient to lean on your RDBMS and let it do the filter and sort for you. If, on the other hand, you want the strings for a variety of groovy purposes then you can filter them on the client side using a couple of algorithms from the C++ standard library:

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

std::vector<std::string> create_test_data()
{
	std::vector<std::string> strings;

	strings.push_back( "aa" );
	strings.push_back( "bb" );
	strings.push_back( "cc" );
	strings.push_back( "ss" );
	strings.push_back( "sss" );
	strings.push_back( "sss" );

	std::random_shuffle( strings.begin(), strings.end() );

	return strings;
}

void print( const std::vector<std::string> &data, std::ostream & to )
{
	std::copy( strings.begin(), strings.end(), std::ostream_iterator<std::string>( to, "\n" ) ) << std::endl;
}

int main()
try
{
	std::vector<std::string> strings( create_test_data() );
	print( strings, std::cout );

	strings.erase(
		std::remove_if(
			strings.begin(),
			strings.end(),
			[]( const std::string &str ){ return str.front() != 's'; } ),
		strings.end() );
	std::sort( strings.begin(), strings.end() );

	print( strings, std::cout );
}
catch( std::exception &e )
{
	std::cout << "Something went wrong: " << e.what() << std::endl;
}
catch( ... )
{
	std::cout << "Something went wrong, no idea what!" << std::endl;
}


The two important lines are:

strings.erase(
	std::remove_if(
		strings.begin(),
		strings.end(),
		[]( const std::string &str ){ return str.front() != 's'; } ),
	strings.end() );
std::sort( strings.begin(), strings.end() );


as they're the lines that filter and sort the strings.

The first one removes any string that doesn't start with an 's' character. I've used a lambda to do the comparison - if you're not using a very up to date compiler you might have to replace that with a function object. Holler if you need some help. As remove_if has some funky behaviour (it can't resize the range it's filtering) you have to resize the vector - which is what erase is doing. Have a look at "Effective STL" by Scott Myers for a good treatment of why this happens.

The second line just sorts the remaining strings into alphabetical order.

Hope this helps, please scream if you need more help with this and don't decide SQL is the way to go!

Cheers,

Ash

Edited to play around with formatting a bit...

modified on Saturday, June 26, 2010 10:11 AM

QuestionRe: sorting alphabetically Pin
David Crow28-Jun-10 5:03
MemberDavid Crow28-Jun-10 5:03 
QuestionData Execution Prevention Pin
T.RATHA KRISHNAN25-Jun-10 21:48
MemberT.RATHA KRISHNAN25-Jun-10 21:48 
AnswerRe: Data Execution Prevention Pin
«_Superman_»26-Jun-10 3:14
professional«_Superman_»26-Jun-10 3:14 
AnswerRe: Data Execution Prevention Pin
MindCoder7927-Jun-10 18:55
MemberMindCoder7927-Jun-10 18:55 
QuestionTesting if Unicode Pin
Hans Dietrich25-Jun-10 20:52
mentorHans Dietrich25-Jun-10 20:52 
AnswerRe: Testing if Unicode Pin
Rajesh R Subramanian25-Jun-10 22:09
professionalRajesh R Subramanian25-Jun-10 22:09 
GeneralRe: Testing if Unicode Pin
Hans Dietrich25-Jun-10 22:54
mentorHans Dietrich25-Jun-10 22:54 
GeneralRe: Testing if Unicode Pin
ThatsAlok27-Jun-10 20:20
MemberThatsAlok27-Jun-10 20:20 
AnswerRe: Testing if Unicode Pin
Richard MacCutchan25-Jun-10 23:12
mveRichard MacCutchan25-Jun-10 23:12 
GeneralRe: Testing if Unicode Pin
Rajesh R Subramanian26-Jun-10 0:38
professionalRajesh R Subramanian26-Jun-10 0:38 
GeneralRe: Testing if Unicode [modified] Pin
Richard MacCutchan26-Jun-10 1:05
mveRichard MacCutchan26-Jun-10 1:05 
AnswerRe: Nothing seems to work Pin
Software_Developer26-Jun-10 3:29
MemberSoftware_Developer26-Jun-10 3:29 
GeneralRe: Nothing seems to work Pin
Richard MacCutchan26-Jun-10 4:35
mveRichard MacCutchan26-Jun-10 4:35 
AnswerRe: Testing if Unicode Pin
Richard MacCutchan26-Jun-10 4:33
mveRichard MacCutchan26-Jun-10 4:33 
GeneralRe: Richards code works. Pin
Software_Developer26-Jun-10 4:57
MemberSoftware_Developer26-Jun-10 4:57 
GeneralRe: Testing if Unicode Pin
Rajesh R Subramanian26-Jun-10 9:56
professionalRajesh R Subramanian26-Jun-10 9:56 
GeneralRe: Testing if Unicode Pin
Niklas L26-Jun-10 12:44
MemberNiklas L26-Jun-10 12:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.