Browse Source

add chap3_2

hanojiang 1 năm trước cách đây
mục cha
commit
293d5403b5

+ 1 - 2
EssentialCpp/EssentialCpp.vcxproj

@@ -127,11 +127,10 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="chap2_8.cpp" />
+    <ClCompile Include="chap3_2.cpp" />
     <ClCompile Include="utils.cpp" />
   </ItemGroup>
   <ItemGroup>
-    <ClInclude Include="chap2_8.h" />
     <ClInclude Include="utils.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 1 - 4
EssentialCpp/EssentialCpp.vcxproj.filters

@@ -18,7 +18,7 @@
     <ClCompile Include="utils.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
-    <ClCompile Include="chap2_8.cpp">
+    <ClCompile Include="chap3_2.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
   </ItemGroup>
@@ -26,8 +26,5 @@
     <ClInclude Include="utils.h">
       <Filter>头文件</Filter>
     </ClInclude>
-    <ClInclude Include="chap2_8.h">
-      <Filter>源文件</Filter>
-    </ClInclude>
   </ItemGroup>
 </Project>

+ 1 - 0
EssentialCpp/chap2_8.h

@@ -1,4 +1,5 @@
 #pragma once
+
 #include "utils.h"
 const int seq_cnt = 1;
 

+ 59 - 0
EssentialCpp/chap3_2.cpp

@@ -0,0 +1,59 @@
+#include <iostream>
+#include <vector>
+//#include <iterator>
+
+using namespace std;
+
+template <typename T>
+void display(const vector<T>& vec, ostream& os=cout) {
+    //const_iterator 敲错了
+    // 编译错误need 'typename' before *** because *** is a dependent scope; https://blog.csdn.net/pb1995/article/details/49532285/
+    typename vector<T>::const_iterator it = vec.begin();
+    typename vector<T>::const_iterator it_end = vec.end();
+
+	for (; it != it_end; ++it) {
+		os << *it << ' ';
+
+	}
+
+	os << endl;
+}
+
+template <typename IteratorType, typename elemType>
+IteratorType myfind(IteratorType first, IteratorType last, const elemType& value) {
+
+    for (; first != last; ++first) {
+        if (value == *first)
+            return first;
+    }
+    return last;
+}
+/*
+template <typename elemType>
+void display_vector(const vector<elemType>& vec, ostream& os = cout, int len = 8)
+{
+    typename vector<elemType>::const_iterator
+        iter = vec.begin(),
+        end_it = vec.end();
+
+    int elem_cnt = 1;
+    while (iter != end_it) {
+        os << *iter++
+            << (!(elem_cnt++ % len) ? '\n' : ' ');  // 一行8个字符串
+        os << endl;
+    }
+}
+*/
+
+
+
+int main() {
+    vector<int> vec = { 12,3,4,5 };
+    display(vec);
+
+    vector<int>::iterator it;
+    it = myfind(vec.begin(), vec.end(), 3);
+
+    cout << *it << endl;
+	return 0;
+}

+ 1 - 0
EssentialCpp/utils.cpp

@@ -30,3 +30,4 @@ void display_message(const string&s, int v1, int v2)
 }
 
 
+

+ 3 - 0
EssentialCpp/utils.h

@@ -1,6 +1,9 @@
 #pragma once
+
 #include <iostream>
 #include <vector>
+#include <iterator>
+
 using namespace std;
 typedef unsigned int u32;