Thursday, November 18, 2010

C++ Map загвар класс

Map нь түлхүүрийн утга (key value) болон түүнд харгалзах утга(mapped value)-ыг хослуулсан хэлбэртэй элементүүдийг агуулагч төрөл юм. Key value нь элементүүдийг бие биенээс ялгаж байдаг бөгөөд утга нь давхардаж болохгүй. Key value, mapped value 2-ын авах утгын төрөл нь ялгаатай байж болно. Маш энгийн жишээ дурдъя. Ашиглагдаж байгаа гар утасны дугаар болгон эзэнтэй, мөн дугаар давхарддагүй гэдгээс дугаарыг key value, эзмшигчийн нэр(мэдээлэл)-ийг maped value болгон харгалзуулж болно. Map-ийг дараах хэлбэртэйгээр зарлана: map<түлхүүр утгын төрөл, харгалзах утгын төрөл> нэр; Ж. map <int, string> mobile;
Key value, mapped value-ийн хосолсон утгыг хамтад нь iterator гэж нэрийдэнэ. Iterator-ийг map<түлхүүр утгын төрөл, харгалзах утгын төрөл>::iterator итераторийн_нэр; гэж зарлана. Итераторын түлхүүр утгыг итераторын_нэр -> first, харгалзах утгыг итераторын_нэр -> second гэж авна.
Гишүүн функцүүд:

1. begin(): map-ийн эхний итераторын утгыг буцаана.
2. clear(): map-ыг цэвэрлэнэ (бүх утгыг устгана).
3. count(): map-д тухайн key бүхий элемент байгаа эсэхийг тодорхойлно. Байвал 1, байхгүй бол 0 гэсэн утга буцаана.
4. empty(): map хоосон эсэхийг тодорхойлно.
5. end(): map-ийн сүүлчийн итераторын утгыг буцаана.
6. erase(): map-аас өгөгдсөн key-тэй итераторыг устгана.
7. find(): өгөгдсөн key-тэй итератор map-д байгаа эсэхийг тодорхойлж, байвал тухайн key бүхий итераторыг буцаана.
8. insert(): map-д итератор нэмнэ.
9. max_size() : map-д байж болох итераторын хамгийн их тоог буцаана.
10. size(): map-д байгаа итераторын тоог буцаана.

Жишээнүүд

// map::begin/end
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;

// show content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

return 0;

}

// map::find
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;

it=mymap.find('b');
mymap.erase (it);
mymap.erase (mymap.find('d'));
// print content:
cout << "elements in mymap:" << endl;
cout << "a => " << mymap.find('a')->second << endl;
cout << "c => " << mymap.find('c')->second << endl;
return 0;
}

// map::insert
#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
pair<map<char,int>::iterator,bool> ret;

// first insert function version (single parameter):
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
ret=mymap.insert (pair<char,int>('z',500) );
if (ret.second==false)
{
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << endl;
}

// second insert function version (with hint position):
it=mymap.begin();
mymap.insert (it, pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, pair<char,int>('c',400)); // no max efficiency inserting

// third insert function version (range insertion):
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));

// showing contents:
cout << "mymap contains:\n";
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;

cout << "anothermap contains:\n";
for ( it=anothermap.begin() ; it != anothermap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}

7 comments:

  1. Hi everyone, Can someone visit my site vimax 3-4 Dot Com, and tell me if im on the right track? if you guys think i should change it. ill start a fresh on my site again. i need feedback thanks!

    ReplyDelete
  2. Ok got my site up vimax 3-4 Dot Com but have not yet advertised anywhere I need some feedback to my domain name also website if its good. Please give me some feedback before I advertise.

    ReplyDelete
  3. Clomid 50mg
    26 Oct 2010 ... buy nolvadex drug buy nolvadex d 20 buy nolvadex and clomid online buy nolvadex ... buy clomid and nolvadex online buy clomid and nolvadex ...
    - 1997 - Cited by 34 - Related articlesClomiphene (CLO) is a substituted triphenylethylene antiestrogen based on its ability to antagonize estrogen-mediated uterine growth in rodents. ...endo.endojournals.org/cgi/content/full/138/5/1794
    Clomid Buy Uk
    Ovulation
    3 Aug 2010 ... Clomid Without Rx Buy Drugs no prescription needed,Online medical ... to back be of cheap clomid (clomiphene) 100 mg are the aids symptoms. ...
    Clomid And Ovulation
    Clomid Success Rates

    ReplyDelete
  4. Buy Dapoxetine
    dapoxetine. 22 October 2010. Aloha!dapoxetine ...
    21 May 2010 ... Honest Reviews Of Vigaplus Does Vigaplus Work? Is Vigaplus A Rip Off? Should You Try Vigaplus? Read This Before You Buy Anything.
    VigaPlus
    1 post - 1 author - Last post: 29 Sep
    Priligy FDA
    Buy-Dapoxetine-Priligy
    Dapoxetine-Trial-Pack
    Buy-Dapoxetine-Hydrochloride
    Quality Assured Online Pharmacy Prescription S S L Secure .... Priligy Dapoxetine. Average customer rating: 0 stars Total votes: 0. Product Details. ...

    ReplyDelete
  5. Then a friend suggested VigaPlus and the very first day I took it, I was able to get a hard and long lasting erection. The results are fantastic" ...
    26 Apr 2009 ... multitudinous versions of viagra title to be genuine fallout, but VigaPlus is a purelly understandable switching of it ...
    2 Sep 2010 ... VigaPlus is perhaps the fastest acting impotence herbal cure – generally the effect is visible within fifteen minutes of taking the pill. ...
    Buy Priligy Australia
    Buy Priligy USA
    Priligy Dapoxetine is an oral medication for premature ejaculation. Buy Priligy online in 30mg 60mg dosages. Priligy Dapoxetine is available at 121doc with ...
    VigaPlus
    Buy-Dapoxetine-Hydrochloride
    VigaPlus pills, an anti-impotency herbal remedy, are formulated by using unique herbs that help in overcoming erectile dysfunction in men.
    Comprar-Dapoxetina

    ReplyDelete
  6. I really like this discussion. I am so glad that I found this wonderful website. What an amazing forum!

    Search Engine Optimization

    ReplyDelete
  7. Thank you, I have recently been searching for info about this subject for a while and yours is the greatest I have found out so far. However, what about the bottom line? Are you sure about the supply?
    At the same time I would reccomend to review this blog - http://slotsgamean.blogspot.com

    ReplyDelete