.


:




:

































 

 

 

 


(map)




-. , apple-. , , .

, . , . . - . . - : map <string, ContactInformation>.

: , , , .

 

#include "stdafx.h"

#include <set>

#include <iostream>

#include <algorithm>

#include <iterator>

#include <string>

#include <map>

#include <sstream>

 

using namespace std;

 

struct ContactInformation

{

string FirstName;

int Age;

string Address;

string Phone;

 

ContactInformation(

const string& firstName,

int age,

const string& address,

const string& phone): FirstName(firstName), Age(age), Address(address), Phone(phone)

{

 

}

 

ContactInformation() // Required by map to return elements

: FirstName("Unknown"), Age(5), Address("Unknown"), Phone("23232323")

{

 

}

 

string ToString() const

{

stringstream stream;

 

stream << "First name: " << FirstName << "; Age: " << Age << "; Address: " << Address << "; Phone: " << Phone << ".";

 

return stream.str();

}

};

 

map<string, ContactInformation> CreateContacts()

{

map<string, ContactInformation> contacts;

 

contacts["Barbara"] = ContactInformation("Barbara", 13, "Ivanovo", "22323");

contacts["Ahmed"] = ContactInformation("Ahmed", 40, "Istanbul", "44333");

 

return contacts;

}

 

void PrintContacts(const map<string, ContactInformation>& contacts)

{

cout << "Contacts\n";

for (auto contactIterator = contacts.begin(); contactIterator!= contacts.end(); ++contactIterator)

{

cout << "Key: " << contactIterator->first << endl;

cout << "Information: " << contactIterator->second.ToString() << endl;

}

// Second more better approach

for (const auto &contactPair: contacts)

{

cout << "Key: " << contactPair.first << endl;

cout << "Information: " << contactPair.second.ToString() << endl;

}

}

 

int _tmain(int argc, _TCHAR* argv[])

{

map<string, ContactInformation> contacts = CreateContacts();

 

// add

PrintContacts(contacts);

 

// check if key exists

if (contacts.count("Barbara") == 1) // check if element exists

cout << "Barbara exists\n";

 

// delete

contacts.erase("Barbara");

 

// modify

contacts["Ahmed"].Address = "USA";

 

PrintContacts(contacts);

 

getchar();

 

return 0;

}





:


: 2015-05-05; !; : 420 |


:

:

: , .
==> ...

1324 - | 936 -


© 2015-2024 lektsii.org - -

: 0.007 .