Click here to Skip to main content
15,885,931 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: A RichEdit without any borders Pin
Rozis29-Jun-10 4:03
Rozis29-Jun-10 4:03 
GeneralRe: A RichEdit without any borders Pin
Niklas L29-Jun-10 10:06
Niklas L29-Jun-10 10:06 
AnswerRe: A RichEdit without any borders Pin
Xeqtr26-Jun-10 10:44
Xeqtr26-Jun-10 10:44 
GeneralRe: A RichEdit without any borders Pin
Rozis29-Jun-10 4:01
Rozis29-Jun-10 4:01 
Questionsorting alphabetically Pin
raju_shiva25-Jun-10 21:53
raju_shiva25-Jun-10 21:53 
AnswerRe: sorting alphabetically Pin
Richard MacCutchan25-Jun-10 22:18
mveRichard MacCutchan25-Jun-10 22:18 
AnswerRe: sorting alphabetically Pin
Shivanand Gupta26-Jun-10 2:40
Shivanand Gupta26-Jun-10 2:40 
AnswerRe: sorting alphabetically [modified] Pin
Aescleal26-Jun-10 4:05
Aescleal26-Jun-10 4: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 4:03
David Crow28-Jun-10 4:03 
QuestionData Execution Prevention Pin
T.RATHA KRISHNAN25-Jun-10 20:48
T.RATHA KRISHNAN25-Jun-10 20:48 
AnswerRe: Data Execution Prevention Pin
«_Superman_»26-Jun-10 2:14
professional«_Superman_»26-Jun-10 2:14 
AnswerRe: Data Execution Prevention Pin
MindCoder7927-Jun-10 17:55
MindCoder7927-Jun-10 17:55 
QuestionTesting if Unicode Pin
Hans Dietrich25-Jun-10 19:52
mentorHans Dietrich25-Jun-10 19:52 
AnswerRe: Testing if Unicode Pin
Rajesh R Subramanian25-Jun-10 21:09
professionalRajesh R Subramanian25-Jun-10 21:09 
GeneralRe: Testing if Unicode Pin
Hans Dietrich25-Jun-10 21:54
mentorHans Dietrich25-Jun-10 21:54 
GeneralRe: Testing if Unicode Pin
ThatsAlok27-Jun-10 19:20
ThatsAlok27-Jun-10 19:20 
AnswerRe: Testing if Unicode PinPopular
Richard MacCutchan25-Jun-10 22:12
mveRichard MacCutchan25-Jun-10 22:12 
GeneralRe: Testing if Unicode Pin
Rajesh R Subramanian25-Jun-10 23:38
professionalRajesh R Subramanian25-Jun-10 23:38 
GeneralRe: Testing if Unicode [modified] Pin
Richard MacCutchan26-Jun-10 0:05
mveRichard MacCutchan26-Jun-10 0:05 
AnswerRe: Nothing seems to work Pin
Software_Developer26-Jun-10 2:29
Software_Developer26-Jun-10 2:29 
GeneralRe: Nothing seems to work Pin
Richard MacCutchan26-Jun-10 3:35
mveRichard MacCutchan26-Jun-10 3:35 
AnswerRe: Testing if Unicode Pin
Richard MacCutchan26-Jun-10 3:33
mveRichard MacCutchan26-Jun-10 3:33 
GeneralRe: Richards code works. Pin
Software_Developer26-Jun-10 3:57
Software_Developer26-Jun-10 3:57 
GeneralRe: Testing if Unicode Pin
Rajesh R Subramanian26-Jun-10 8:56
professionalRajesh R Subramanian26-Jun-10 8:56 
GeneralRe: Testing if Unicode Pin
Niklas L26-Jun-10 11:44
Niklas L26-Jun-10 11: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.