板凳——————————————————c++(50)

//The c++ standard Library p345
//2020年06月09日 19时54分54秒
#include
#include
#include
#include
#include
int main()
{
std::mapstd::string,double coll { { “tim”, 9.9 },
{ “struppi”, 11.77 }
} ;

    // square the value of each element:
    std::for_each (coll.begin(), coll.end(),
	      [] (std::pair<const std::string,double>& elem) {  // 调用元素 by reference, 使value 得以改动
	            elem.second *= elem.second;  //  计算平方
	      });

    // print each element:
    std::for_each (coll.begin(), coll.end(),                              // 避免产生非必要的拷贝
	      [] (const std::map<std::string,double>::value_type& elem) { // 调用const reference 
	            std::cout << elem.first << ": " << elem.second << std::endl; // 打印结果
	      });
     //  The c++ standard Library p346        
	typedef std::map<std::string,float> StringFloatMap;

    StringFloatMap stocks;      // create empty container

    // insert some elements
    stocks["BASF"] = 369.50;
    stocks["VW"] = 413.50;
    stocks["Daimler"] = 819.00;
    stocks["BMW"] = 834.00;
    stocks["Siemens"] = 842.20;

    // print all elements
    StringFloatMap::iterator pos;
    std::cout << std::left;  // left-adjust values
    for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
	std::cout << "stock: " << std::setw(12) << pos->first
	     << "price: " << pos->second << std::endl;
    }
    std::cout << std::endl;

    // boom (all prices doubled)
    for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
	pos->second *= 2;
    }

    // print all elements
    for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
	std::cout << "stock: " << std::setw(12) << pos->first
	     << "price: " << pos->second << std::endl;
    }
    std::cout << std::endl;

    // rename key from "VW" to "Volkswagen"
    // - provided only by exchanging element
    stocks["Volkswagen"] = stocks["VW"];
    stocks.erase("VW");

    // print all elements
    for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
	std::cout << "stock: " << std::setw(12) << pos->first
	     << "price: " << pos->second << std::endl;
    }
    
    //  The c++ standard Library p346
    // create multimap as string/string dictionary
    std::multimap<std::string,std::string> dict;

    // insert some elements in random order
    dict.insert ( { {"day","Tag"}, {"strange","fremd"},
	            {"car","Auto"}, {"smart","elegant"},
	            {"trait","Merkmal"}, {"strange","seltsam"},
	            {"smart","raffiniert"}, {"smart","klug"},
	            {"clever","raffiniert"} } );

    // print all elements
    std::cout.setf (std::ios::left, std::ios::adjustfield);
    std::cout << ' ' << std::setw(10) << "english " << ' '
	 << "german " << std::endl;
    std::cout << std::setfill('-') << std::setw(20) << ""
	 << std::setfill('-') << std::setw(20) << ""  << std::endl;
    for ( const auto& elem : dict ) {
	std::cout << ' ' << std::setw(10) << elem.first
	     << elem.second << std::endl;
    }
    std::cout << std::endl;

    // print all values for key "smart"
    std::string word("smart");
    std::cout << word << ": " << std::endl;
    for (auto pos = dict.lower_bound(word);
	 pos != dict.upper_bound(word);
	 ++pos) {
	    std::cout << "    " << pos->second << std::endl;
    }

    	// print all keys for value "raffiniert"
    word = ("raffiniert");
    std::cout << word << ": " << std::endl;
    for (const auto& elem : dict) {
	if (elem.second == word) {
	    std::cout << "    " << elem.first << std::endl;
	}
    }
//  The c++ standard Library p346

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c12 c12.cpp
wannian07@wannian07-PC:~$ ./c12
struppi: 138.533
tim: 98.01

stock: BASF price: 369.5
stock: BMW price: 834
stock: Daimler price: 819
stock: Siemens price: 842.2
stock: VW price: 413.5

stock: BASF price: 739
stock: BMW price: 1668
stock: Daimler price: 1638
stock: Siemens price: 1684.4
stock: VW price: 827

stock: BASF price: 739
stock: BMW price: 1668
stock: Daimler price: 1638
stock: Siemens price: 1684.4
stock: Volkswagen price: 827

english german

car-------Auto
clever----raffiniert
day-------Tag
smart-----elegant
smart-----raffiniert
smart-----klug
strange—fremd
strange—seltsam
trait-----Merkmal

smart:
elegant
raffiniert
klug
raffiniert:
clever
smart

gedit 默认只是提供了“自动缩进”功能。要多行缩进很简单:

(1)让许多行缩进一层:用鼠标选中那些行,按一下 Tab 键就可以了。

(2)让许多行减少缩进一层:用鼠标选中那些行,按一下 Shift+Tab 键就可以了。

visual studio code 的好处在于真正的跨平台,以前我在Ubuntu里面使用gedit及其各种插件,在 Windows 上又换成 notepad++,在Mac上又要用其它工具,现在都可以使用visual studio code了(不是笨重的visual studio,而是visual studio code,几十M大小)

下载 https://github.com/gmate/gmate/tree/master/plugins/gedit3/folding 里面的两个文件 folding.plugin 与 folding.py,放到 ~/.local/share/gedit/plugins/ 目录里面,然后在 gedit 插件里面勾选它,就可以使用了。
*/

// professional c++ 4th edition p394
#include
#include
#include
#include

class BuddyList final
{
public:
// Adds buddy as a friend of name.
void addBuddy(const std::string& name, const std::string& buddy);

// Removes buddy as a friend of name
void removeBuddy(const std::string& name, const std::string& buddy);

// Returns true if buddy is a friend of name, false otherwise.
bool isBuddy(const std::string& name, const std::string& buddy) const;

// Retrieves a list of all the friends of name.
std::vector<std::string> getBuddies(const std::string& name) const;

private:
std::multimap<std::string, std::string> mBuddies;
};

void BuddyList::addBuddy(const std::string& name, const std::string& buddy)
{

if (!isBuddy(name, buddy)) {
	mBuddies.insert({ name, buddy }); // Using initializer_list
}

}

void BuddyList::removeBuddy(const std::string& name, const std::string& buddy)
{
auto begin = mBuddies.lower_bound(name); // Start of the range
auto end = mBuddies.upper_bound(name); // End of the range

for (auto iter = begin; iter != end; ++iter) {
	if (iter->second == buddy) {
		// We found a match! Remove it from the map.
		mBuddies.erase(iter);
		break;
	}
}

}

bool BuddyList::isBuddy(const std::string& name, const std::string& buddy) const
{
// Obtain the beginning and end of the range of elements with
// key ‘name’ using equal_range(), and C++17 structured bindings.
auto [begin, end] = mBuddies.equal_range(name);

for (auto iter = begin; iter != end; ++iter) {
	if (iter->second == buddy) {
		// We found a match!
		return true;
	}
}
// No matches
return false;

}

std::vectorstd::string BuddyList::getBuddies(const std::string& name) const
{
// Obtain the beginning and end of the range of elements with
// key ‘name’ using equal_range(), and C++17 structured bindings.
auto [begin, end] = mBuddies.equal_range(name);

std::vector<std::string> buddies;
for (auto iter = begin; iter != end; ++iter) {
	buddies.push_back(iter->second);
}

return buddies;

}
//http://www.cplusplus.com/reference/map/multimap/multimap/
bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};
int main()
{
BuddyList buddies;

buddies.addBuddy("Harry Potter", "Ron Weasley");
buddies.addBuddy("Harry Potter", "Hermione Granger");
buddies.addBuddy("Harry Potter", "Hagrid");
buddies.addBuddy("Harry Potter", "Draco Malfoy");
// That's not right! Remove Draco.
buddies.removeBuddy("Harry Potter", "Draco Malfoy");

buddies.addBuddy("Hagrid", "Harry Potter");
buddies.addBuddy("Hagrid", "Ron Weasley");
buddies.addBuddy("Hagrid", "Hermione Granger");

auto harrysFriends = buddies.getBuddies("Harry Potter");

std::cout << "Harry's friends: " << std::endl;
for (const auto& name : harrysFriends) {
	std::cout << "\t" << name << std::endl;
}

// http://www.cplusplus.com/reference/map/multimap/multimap/
std::multimap<char,int> first;

  first.insert(std::pair<char,int>('a',10));
  first.insert(std::pair<char,int>('b',15));
  first.insert(std::pair<char,int>('b',20));
  first.insert(std::pair<char,int>('c',25));

  std::multimap<char,int> second (first.begin(),first.end());

  std::multimap<char,int> third (second);

  std::multimap<char,int,classcomp> fourth;                 // class as Compare

  bool(*fn_pt)(char,char) = fncomp;
  std::multimap<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as comp

   std::multimap<char,int> mymultimap;
  std::multimap<char,int>::iterator it2,itlow,itup;

  mymultimap.insert(std::make_pair('a',10));
  mymultimap.insert(std::make_pair('b',121));
  mymultimap.insert(std::make_pair('c',1001));
  mymultimap.insert(std::make_pair('c',2002));
  mymultimap.insert(std::make_pair('d',11011));
  mymultimap.insert(std::make_pair('e',44));

  itlow = mymultimap.lower_bound ('b');  // itlow points to b   从b 开始
  itup = mymultimap.upper_bound ('d');   // itup points to e (not d)  到d 结束

  // print range [itlow,itup):
  for (it2=itlow; it2!=itup; ++it2)
    std::cout << (*it2).first << " => " << (*it2).second << '\n';

// http://www.cplusplus.com/reference/map/multimap/value_comp/
mymultimap.insert(std::make_pair(‘x’,101));
mymultimap.insert(std::make_pair(‘y’,202));
mymultimap.insert(std::make_pair(‘y’,252));
mymultimap.insert(std::make_pair(‘z’,303));

  std::cout << "mymultimap contains:\n";

  std::pair<char,int> highest = *mymultimap.rbegin();          // last element

  std::multimap<char,int>::iterator it1 = mymultimap.begin();
  do {
    std::cout << (*it1).first << " => " << (*it1).second << '\n';
  } while ( mymultimap.value_comp()(*it1++, highest) );


return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c12 c12.cpp
wannian07@wannian07-PC:~$ ./c12
Harry’s friends:
Ron Weasley
Hermione Granger
Hagrid

b => 121
c => 1001
c => 2002
d => 11011

x => 101
y => 202
y => 252
z => 303

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值