473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sort list<string> stringList??

Ok, I have a list of strings (list<string> stringList) that I want to
sort alphabetcially, only "sort(stringLis t.begin(), stringList.end( ));
) does not work. Any insight would be helpful. Thanks!

Oct 21 '05 #1
3 8493
Ok, had wrong code... needed:
stringList.sort ();
instead. Thanks.

Oct 21 '05 #2

<aq******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
| Ok, I have a list of strings (list<string> stringList) that I want to
| sort alphabetcially, only "sort(stringLis t.begin(), stringList.end( ));
| ) does not work. Any insight would be helpful. Thanks!
|

Both list and string are not defined in the standard. Try std::string
and std::list.

Why use a container like a std::list if a std::set does this for you
transparently using a predicate (default pred: std::less<>)?

#pragma warning(disable :4786)
#include <string>
#include <set>
#include <iterator>
#include <algorithm>
#include <ostream>
#include <iostream>

int main()
{
/* default:
std::set< std::string,
std::less<std:: string>,
std::allocator< std::string> > stringset;
*/
std::set< std::string > stringset;
stringset.inser t("string d");
stringset.inser t("string a"); // sorted with std::less<std:: string>
stringset.inser t("string c"); // sorted ...
stringset.inser t("string b"); // sorted ...

std::copy( stringset.begin (),
stringset.end() ,
std::ostream_it erator<std::str ing>( std::cout,
"\n") );
std::cout << std::endl;

return 0;
}

/*
string a
string b
string c
string d
*/

Oct 22 '05 #3
On 2005-10-22, Peter_Julian <pj@antispam.co digo.ca> wrote:
Why use a container like a std::list if a std::set does this
for you transparently using a predicate (default pred:
std::less<>)?


It depends on his requirements:

stringset.inser t("string d");
stringset.inser t("string a"); // sorted with std::less<std:: string>
stringset.inser t("string a"); // doesn't get inserted at all.

Moreover, he may not need or want it to be sorted all the time.

--
Neil Cerutti
Oct 24 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15182
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the generic list...is not clear to me. Any suggestions? System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>(); foreach(string s in myAnotherArray)
6
46188
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: <BEGIN CODE> List<stringkeyNameList = new List<string>(); foreach (string keyName in this.myDictionary.Keys)
4
23967
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
4
2957
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
1
3770
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void addString (const std::string s); void print_strings () const;
12
2452
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses a Static Object. In this object is a generic List<String>. For every page request this list is looped over only reading not writing each value.
2
5895
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the dictionary essentially creates the following array: Key Value
4
3954
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
3
3453
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index is one in which each phrase is indexed by every word in the phrase. */
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.