[glibmm/c++11] C++11: Use range-based for loops.



commit 442675b911245d36461f45644c1c2be1aacc3fab
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Jun 26 10:46:22 2013 +0200

    C++11: Use range-based for loops.

 examples/dbus/client_bus_listnames.cc |    4 ++--
 examples/keyfile/main.cc              |    4 ++--
 examples/markup/parser.cc             |    4 ++--
 examples/network/resolver.cc          |   16 +++++++---------
 examples/options/main.cc              |    8 ++++----
 5 files changed, 17 insertions(+), 19 deletions(-)
---
diff --git a/examples/dbus/client_bus_listnames.cc b/examples/dbus/client_bus_listnames.cc
index b44c933..0a037cb 100644
--- a/examples/dbus/client_bus_listnames.cc
+++ b/examples/dbus/client_bus_listnames.cc
@@ -60,8 +60,8 @@ void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
 
     std::cout << "The names on the message bus are:" << std::endl;
 
-    for(unsigned i = 0; i < names.size(); i++)
-      std::cout << names[i] << "." << std::endl;
+    for(const auto& i : names)
+      std::cout << i << "." << std::endl;
   }
   catch(const Glib::Error& error)
   {
diff --git a/examples/keyfile/main.cc b/examples/keyfile/main.cc
index 4562044..f5e3659 100644
--- a/examples/keyfile/main.cc
+++ b/examples/keyfile/main.cc
@@ -68,8 +68,8 @@ int main(int, char**)
   {
     const auto values = keyfile.get_integer_list("Another Group", "Numbers");
 
-    for(auto p = values.begin(); p != values.end(); ++p)
-      std::cout << "Number list value: item=" << *p << std::endl;
+    for(const auto& p : values)
+      std::cout << "Number list value: item=" << p << std::endl;
   }
   catch(const Glib::KeyFileError& ex)
   {
diff --git a/examples/markup/parser.cc b/examples/markup/parser.cc
index 08e3342..d3538be 100644
--- a/examples/markup/parser.cc
+++ b/examples/markup/parser.cc
@@ -84,9 +84,9 @@ void DumpParser::on_start_element(Glib::Markup::ParseContext&,
   indent();
   std::cout << '<' << element_name;
 
-  for(auto p = attributes.begin(); p != attributes.end(); ++p)
+  for(const auto& p : attributes)
   {
-    std::cout << ' ' << p->first << "=\"" << p->second << '"';
+    std::cout << ' ' << p.first << "=\"" << p.second << '"';
   }
 
   std::cout << ">\n";
diff --git a/examples/network/resolver.cc b/examples/network/resolver.cc
index 54196f6..9361c14 100644
--- a/examples/network/resolver.cc
+++ b/examples/network/resolver.cc
@@ -95,10 +95,9 @@ print_resolved_addresses (const Glib::ustring& name,
 {
     G_LOCK (response);
     std::cout << Glib::ustring::compose ("Name:    %1\n", name);
-    for (auto iter = addresses.begin ();
-         iter != addresses.end (); ++iter)
+    for (const auto& i : addresses)
     {
-        std::cout << Glib::ustring::compose ("Address: %1\n", (*iter)->to_string ());
+        std::cout << Glib::ustring::compose ("Address: %1\n", i->to_string ());
     }
     std::cout << std::endl;
 
@@ -112,15 +111,14 @@ print_resolved_service (const Glib::ustring& service,
 {
     G_LOCK (response);
     std::cout << Glib::ustring::compose ("Service: %1\n", service);
-    for (auto iter = targets.begin ();
-         iter != targets.end (); ++iter)
+    for (const auto& i : targets)
     {
         std::cout <<
             Glib::ustring::compose ("%1:%2 (pri %3, weight %4)\n",
-                                    iter->get_hostname (),
-                                    iter->get_port (),
-                                    iter->get_priority (),
-                                    iter->get_weight ());
+                                    i.get_hostname (),
+                                    i.get_port (),
+                                    i.get_priority (),
+                                    i.get_weight ());
     }
     std::cout << std::endl;
 
diff --git a/examples/options/main.cc b/examples/options/main.cc
index f0f0669..eb0d4f6 100644
--- a/examples/options/main.cc
+++ b/examples/options/main.cc
@@ -222,17 +222,17 @@ int main(int argc, char** argv)
     
   //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
   std::cout << "  list = ";
-  for(auto iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
+  for(const auto& i : group.m_arg_list)
   {
-    std::cout << *iter << ", ";
+    std::cout << i << ", ";
   }
   std::cout << std::endl;
 
   //This one shows the remaining arguments on the command line, which had no name= form:
   std::cout << "  remaining = ";
-  for(auto iter = group.m_remaining_list.begin(); iter != group.m_remaining_list.end(); ++iter)
+  for(const auto& i : group.m_remaining_list)
   {
-    std::cout << *iter << ", ";
+    std::cout << i << ", ";
   }
   std::cout << std::endl;
  


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]