C + + container learning, with struct sort and set to encounter

Source: Internet
Author: User

Recently learn C + + containers, accumulate a bit. Set and Multiset are described below, and the structure is sorted using sort. C + + is a long way to fix it!

First, the structure of the order

Sort_struct.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <algorithm>using namespace std; typedef struct example{    int elem1;    char elem2;} Example;bool Comparison (example A,example b) {    return a.elem1>b.elem1;} int main () {    int N;    cin>>n;    Vector<example> Array (N);    for (int i=0;i<n;i++)    {        cin>>array[i].elem1>>array[i].elem2;    }    Sort (Array.begin (), Array.end (), comparison);    for (int i=0;i<n;i++)    {        cout<<array[i].elem1<< "" "<<array[i].elem2<<endl;    }        return 0;}


Second, set application

Set, as the name implies, is the concept of a collection that does not allow repeating elements. Multiset allowed values to be repeated, it can be counted. The other uses are consistent.

Set.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "iostream" #include "string" #include "set" #include <algorithm>using namespace std; int _tmain (int argc, _tchar* argv[]) {multiset<int> s;multiset<int>::iterator si; S.insert (4); S.insert (4); S.insert (3); for (Si=s.begin (); Si!=s.end (); si++) cout << *si << endl;cout<<s.count (1) <<endl; return 0;}


C + + container learning, with struct sort and set to encounter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.