A+B for Matrices 及 C++ transform的用法
題目大意:給定兩個矩陣,矩陣的最大大小是M*N(小于等于10),矩陣元素的值的絕對值小于等于100,求矩陣相加后全0的行以及列數。
1 #include<iostream> 2 using namespace std; 3 #define N 10 4 5 int main() 6 { 7 int n,m,i,j,a[N][N],b[N][N],s; 8 while(cin>>m) 9 { if(m==0) break; 10 cin>>n; 11 for(i=0;i<m;i++) 12 for(j=0;j<n;j++) 13 cin>>a[i][j]; 14 for(i=0;i<m;i++) 15 for(j=0;j<n;j++) 16 { cin>>b[i][j]; 17 a[i][j]+=b[i][j]; 18 } 19 s=0; 20 for(i=0;i<m;i++) 21 { for(j=0;j<n;j++) 22 if(a[i][j]!=0) break; 23 if(j==n) s++; 24 } 25 for(j=0;j<n;j++) 26 { for(i=0;i<m;i++) 27 if(a[i][j]!=0) break; 28 if(i==m) s++; 29 } 30 cout<<s<<endl; 31 } 32 return 0; 33 }在網上看到一個大神用純C++的思想寫了如下代碼:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 int add(int a, int b){ 7 return a+b; 8 } 9 10 int main(){ 11 vector<int> ivec1; 12 vector<int> ivec2; 13 14 int m, n, sum, data; 15 while(cin >> m){ 16 if(0 == m){ 17 break; 18 } 19 cin >> n; 20 sum = m * n; 21 for(int i=0; i!=sum; ++i){ 22 cin >> data; 23 ivec1.push_back(data); 24 } 25 for(int i=0; i!=sum; ++i){ 26 cin >> data; 27 ivec2.push_back(data); 28 } 29 transform(ivec1.begin(), ivec1.end(), ivec2.begin(), ivec1.begin(), add); 30 int count = 0; 31 int temp; 32 for(int i=0; i!=m; ++i){ 33 temp = 0; 34 for (int j=0; j!=n; ++j){ 35 temp += ivec1[i*n + j]; 36 } 37 if (0 == temp){ 38 count++; 39 } 40 } 41 for(int i=0; i!=n; ++i){ 42 temp = 0; 43 for (int j=0; j!=m; ++j){ 44 temp += ivec1[j*n + i]; 45 } 46 if (0 == temp){ 47 count++; 48 } 49 } 50 cout << count << endl; 51 ivec1.resize(0); 52 ivec2.resize(0); 53 } 54 return 0; 55 }?上面出現了transform的用法,在這里介紹下其用法:
1 /* 2 template < class InputIterator, class OutputIterator, class UnaryOperator > 3 OutputIterator transform ( InputIterator first1, // 源容器的起始地址 4 InputIterator last1, // 源容器的終止地址 5 OutputIterator result, // 目標容器的起始地址 6 UnaryOperator op ); // 函數指針 7 // typedef 目標容器元素類型 (*UnaryOperator)(源容器元素類型); 8 9 template < class InputIterator1, class InputIterator2, 10 class OutputIterator, class BinaryOperator > 11 OutputIterator transform ( InputIterator1 first1, // 源容器1的起始地址 12 InputIterator1 last1, // 源容器1的終止地址 13 InputIterator2 first2, // 源容器2的起始地址,元素個數與1相同 14 OutputIterator result, // 目標容器的起始地址,元素個數與1相同 15 BinaryOperator binary_op ); // 函數指針 16 // typedef 目標容器元素類型 (*BinaryOperator)(源容器1元素類型,源容器2元素類型); 17 //*//// 18 19 #include <iostream> 20 #include <algorithm> 21 #include <vector> 22 #include <string> 23 using namespace std; 24 25 int op_increase (int i) 26 { 27 return i+1; 28 } 29 30 int op_sum (int i, int j) 31 { 32 return i+j; 33 } 34 35 int to_upper(int c) 36 { 37 if (islower(c)) 38 { 39 return (c-32); 40 } 41 42 return c; 43 } 44 45 int to_lower(int c) 46 { 47 if (isupper(c)) 48 { 49 return c+32; 50 } 51 52 return c; 53 } 54 55 int main () { 56 vector<int> first; 57 vector<int> second; 58 vector<int>::iterator it; 59 60 // set some values: 61 for (int i=1; i<6; i++) first.push_back (i*10); // first: 10 20 30 40 50 62 63 ///將first容器的元素加1賦值給second容器 64 second.resize(first.size()); // allocate space !!!必須預先設置一個大小與first相同 65 transform (first.begin(), first.end(), second.begin(), op_increase); // second: 11 21 31 41 51 66 cout << "second contains:"; 67 for (it=second.begin(); it!=second.end(); ++it) 68 { 69 cout << " " << *it; 70 } 71 cout << endl; 72 //* 73 74 ///將first容器的元素與second容器的元素相加,并將得到的結果重新賦值給first 75 transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // first: 21 41 61 81 101 76 cout << "first contains:"; 77 for (it=first.begin(); it!=first.end(); ++it) 78 cout << " " << *it; 79 cout << endl; 80 //*// 81 82 ///大小寫轉換/ 83 string strsrc("Hello, World!"); 84 string strdest; 85 strdest.resize(strsrc.size()); // !!!必須預先設置一個大小與strsrc相同 86 transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_upper); // 轉換為大寫 87 cout << strdest << endl; 88 89 transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_lower); // 轉換為小寫 90 cout << strdest << endl; 91 //*/ 92 93 return 0; 94 }?
?
?
我們已經了解了一種區間元素交換swap_ranges函數,現在我們再來學習另外一種區間元素交換transform。該算法用于實現容器元素的變 換操作。有如下兩個使用原型,一個將迭代器區間[first,last)中元素,執行一元函數對象op操作,交換后的結果放在 [result,result+(last-first))區間中。另一個將迭代器區間[first1,last1)的元素*i,依次與 [first2,first2+(last-first))的元素*j,執行二元函數操作binary_op(*i,*j),交換結果放在 [result,result+(last1-first1))。
???? 函數原型:
[cpp] view plaincopy???? 參數說明:
first1, last1
指出要進行元素變換的第一個迭代器區間 [first1,last1)。
first2
指出要進行元素變換的第二個迭代器區間的首個元素的迭代器位置,該區間的元素個數和第一個區間相等。
?
result
指出變換后的結果存放的迭代器區間的首個元素的迭代器位置
op
用一元函數對象op作為參數,執行其后返回一個結果值。它可以是一個函數或對象內的類重載operator()。
binary_op
用二元函數對象binary_op作為參數,執行其后返回一個結果值。它可以是一個函數或對象內的類重載operator()。
????? 程序示例:
[cpp] view plaincopy轉載于:https://www.cnblogs.com/heyonggang/p/3263493.html
總結
以上是生活随笔為你收集整理的A+B for Matrices 及 C++ transform的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dagger2和MVParms的学习
- 下一篇: s3c2440移植MQTT