返回列表 發帖

vector 練習 2

本帖最後由 tonyh 於 2023-10-20 21:38 編輯
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v;
  4. int main()
  5. {
  6.     v.push_back(7);   //新增元素
  7.     v.push_back(2);
  8.     v.push_back(1);
  9.     v.push_back(3);
  10.     v.push_back(11);
  11.     v.push_back(5);
  12.     sort(v.begin(), v.end());   //基礎排序
  13.     for(int i: v)
  14.         cout<<i<<" ";
  15.     cout<<endl;
  16.     cout<<"------------"<<endl;
  17.     auto it=lower_bound(v.begin(), v.end(), 7);  //大於或等於目標對象的最小值的位址
  18.     cout<<*it<<endl;     //該位址的值
  19.     it=upper_bound(v.begin(), v.end(), 7);      //大於目標對象的最小值的位址
  20.     cout<<*it<<endl;
  21.     cout<<"------------"<<endl;
  22.     it=upper_bound(v.begin(), v.end(), 9);
  23.     cout<<*it<<endl;   //11
  24.     cout<<*(--it)<<endl;   //7
  25.     it=lower_bound(v.begin(), v.end(), 3);
  26.     cout<<*(++it)<<endl;   //5
  27.     cout<<"------------"<<endl;
  28.     it=lower_bound(v.begin(), v.end(), 3);
  29.     cout<<it-v.begin()<<endl;  //取得索引值
  30.     it=find(v.begin(), v.end(), 2);   //非二分搜尋法, 效能較差, 適合用於未排序的資料。
  31.     cout<<it-v.begin()<<endl;
  32.     cout<<"------------"<<endl;
  33.     it=find(v.begin(), v.end(), 12);   //找不到時會回傳end()指向的位址
  34.     if(it==v.end())
  35.         cout<<"no find"<<endl;
  36.     it=upper_bound(v.begin(), v.end(), 12);
  37.     if(it==v.end())
  38.         cout<<"no find"<<endl;
  39.     return 0;
  40. }
  41. //1 2 3 5 7 11
複製代碼

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     cin.tie(0);
  6.     cin.sync_with_stdio(0);
  7.     vector<int> v;
  8.     v.push_back(7);
  9.     v.push_back(2);
  10.     v.push_back(1);
  11.     v.push_back(3);
  12.     v.push_back(11);
  13.     v.push_back(5);
  14.     sort(v.begin(),v.end());
  15.     for(int i:v)
  16.         cout<<i<<" ";
  17.     cout<<endl;
  18.     for(auto i=v.begin(); i!=v.end(); i++)
  19.         cout<<*i<<" ";
  20.     cout<<endl;
  21.     vector<int>::iterator it=lower_bound(v.begin(),v.end(),7);
  22.     cout<<*it<<endl;
  23.     it=upper_bound(v.begin(),v.end(),7);
  24.     cout<<*it<<endl;
  25.     it--;
  26.     cout<<*(++it)<<endl;
  27.     cout<<*it<<endl;
  28.     it=find(v.begin(), v.end(), 2);
  29.     cout<<*it<<endl;
  30.      cout<<it-v.begin()<<endl;
  31.      it=find(v.begin(),v.end(),55);
  32.      if(it==v.end())
  33.      cout<<"no find";
  34.     return 0;
  35. }
複製代碼

TOP

本帖最後由 林祐霆 於 2023-10-27 19:14 編輯
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v;
  4. bool compare(int a, int b)
  5. {
  6.     return a>b;
  7. }
  8. int main()
  9. {
  10.     v.push_back(7);
  11.     v.push_back(2);
  12.     v.push_back(1);
  13.     v.push_back(3);
  14.     v.push_back(11);
  15.     v.push_back(5);

  16.     sort(v.begin(), v.end());

  17.     for(int i: v)
  18.         cout<<i<<" ";
  19.     cout<<endl;
  20.     cout<<"-------"<<endl;
  21.     auto it=lower_bound(v.begin(), v.end(),7); //>=9最接近的位置
  22.     cout<<*it<<endl;  //*: 取該位置的值
  23.     it=upper_bound(v.begin(), v.end(), 7); //>9最接近的位置
  24.     cout<<*it<<endl;

  25.     cout<<"-------"<<endl;
  26.     it=upper_bound(v.begin(), v.end(), 9);
  27.     cout<<*it<<endl;
  28.     cout<<*(--it)<<endl;

  29.     cout<<"-------"<<endl;
  30.     it=lower_bound(v.begin(), v.end(),3);
  31.     cout<<*(++it)<<endl;
  32.     cout<<it-v.begin()<<endl;

  33.     cout<<"------------"<<endl;
  34.     it=find(v.begin(), v.end(), 12);   //找不到時會回傳end()指向的位址
  35.     if(it==v.end())
  36.         cout<<"no find"<<endl;
  37.     it=upper_bound(v.begin(), v.end(), 12);
  38.     if(it==v.end())
  39.         cout<<"no find"<<endl;
  40.     return 0;
  41.     //1 2 3 5 7 11
  42. }
複製代碼
林祐霆

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v;
  4. int main()
  5. {
  6.     v.push_back(7);
  7.     v.push_back(2);
  8.     v.push_back(1);
  9.     v.push_back(3);
  10.     v.push_back(11);
  11.     v.push_back(5);
  12.     sort(v.begin(), v.end());   //排序
  13.     for(int i: v)
  14.         cout<<i<<" ";
  15.     cout<<endl;
  16.     cout<<"------------"<<endl;
  17.     auto it=lower_bound(v.begin(), v.end(), 7);  //>=7
  18.     cout<<*it<<endl;     //該位址的值
  19.     it=upper_bound(v.begin(), v.end(), 7);      //>7
  20.     cout<<*it<<endl;
  21.     cout<<"------------"<<endl;
  22.     it=upper_bound(v.begin(), v.end(), 9);
  23.     cout<<*it<<endl;   //11
  24.     cout<<*(--it)<<endl;   //7
  25.     it=lower_bound(v.begin(), v.end(), 3);
  26.     cout<<*(++it)<<endl;   //5
  27.     cout<<"------------"<<endl;
  28.     it=lower_bound(v.begin(), v.end(), 3);
  29.     cout<<it-v.begin()<<endl;  //取得索引值
  30.     it=find(v.begin(), v.end(), 2);   //非二分搜尋法, 效能較差, 適合用於未排序的資料。
  31.     cout<<it-v.begin()<<endl;
  32.     cout<<"------------"<<endl;
  33.     it=find(v.begin(), v.end(), 12);  
  34.     if(it==v.end())
  35.         cout<<"no find"<<endl;
  36.     it=upper_bound(v.begin(), v.end(), 12);
  37.     if(it==v.end())
  38.         cout<<"no find"<<endl;
  39.     return 0;
  40. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v;
  4. int main()
  5. {
  6.     v.push_back(7);   //新增元素
  7.     v.push_back(2);
  8.     v.push_back(1);
  9.     v.push_back(3);
  10.     v.push_back(11);
  11.     v.push_back(5);
  12.     sort(v.begin(), v.end());   //基礎排序
  13.     for(int i: v)
  14.         cout<<i<<" ";
  15.     cout<<endl;
  16.     cout<<"------------"<<endl;
  17.     auto it=lower_bound(v.begin(), v.end(), 7);  //大於或等於目標對象的最小值的位址
  18.     cout<<*it<<endl;     //該位址的值
  19.     it=upper_bound(v.begin(), v.end(), 7);      //大於目標對象的最小值的位址
  20.     cout<<*it<<endl;
  21.     cout<<"------------"<<endl;
  22.     it=upper_bound(v.begin(), v.end(), 9);
  23.     cout<<*it<<endl;   //11
  24.     cout<<*(--it)<<endl;   //7
  25.     it=lower_bound(v.begin(), v.end(), 3);
  26.     cout<<*(++it)<<endl;   //5
  27.     cout<<"------------"<<endl;
  28.     it=lower_bound(v.begin(), v.end(), 3);
  29.     cout<<it-v.begin()<<endl;  //取得索引值
  30.     it=find(v.begin(), v.end(), 2);   //非二分搜尋法, 效能較差, 適合用於未排序的資料。
  31.     cout<<it-v.begin()<<endl;
  32.     cout<<"------------"<<endl;
  33.     it=find(v.begin(), v.end(), 12);   //找不到時會回傳end()指向的位址
  34.     if(it==v.end())
  35.         cout<<"no find"<<endl;
  36.     it=upper_bound(v.begin(), v.end(), 12);
  37.     if(it==v.end())
  38.         cout<<"no find"<<endl;
  39.     return 0;
  40. }
複製代碼

TOP

返回列表