如何使用STL将数组转换为矢量

How to convert arrays to vectors using STL

本文关键字:转换 数组 何使用 STL      更新时间:2023-10-16

此代码是一个使用数组的线性搜索程序。出于好奇,我想知道如何使用STL矢量代替数组来重写这些代码,但仍然具有相同的输出。

#include <iostream>
#include <string>
using namespace std;
template <typename T>
int linearSearch(T list[], int key, int arraySize)
{
  for (int i = 0; i < arraySize; i++)
  {
    if (key == list[i])
      return i;
  }
  return -1;
}
int main()
{
  int intArray[] =
  {
    1, 2, 3, 4, 8, 15, 23, 31
  };
  cout << "linearSearch(intArray, 3, 8) is " << linearSearch(intArray, 3, 8) << endl;
  cout << "linearSearch(intArray, 10, 8) is " << linearSearch(intArray, 10, 8) << endl;
  return 0;
}

您可以通过更改参数类型和main来实现。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
int linearSearch(vector<T> list, int key)
{
   for (size_t i = 0; i < list.size(); i++)
   {
      if (key == list[i])
        return i;
   }
   return -1;
}
int main()
{
  int intArray[] =
  {
    1, 2, 3, 4, 8, 15, 23, 31
   };
   vector<int> list(intArray, intArray+8);
   cout << "linearSearch(list, 3,) is " << linearSearch(list, 3) << endl;
   cout << "linearSearch(list, 10) is " << linearSearch(list, 10) << endl;
   return 0;
}

这可以工作(它基于STL实现(:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename ForwardIter, typename Type>
int linearSearch(ForwardIter beg, ForwardIter end, Type key )
{
  int i = 0;
  for (;beg != end; ++beg)
  {
    if (key == *beg)
      return i;
    i++;
  }
  return -1;
}
int main()
{
  vector< int > vec = { 1, 2, 3, 4, 5, 6, 7 };
  cout << "linearSearch 1 is " << linearSearch(vec.begin(), vec.end(), 4) << endl;
  cout << "linearSearch 2 is " << linearSearch(vec.begin()+2, vec.end(), 1) << endl;
  return 0;
}

注意:它也可以用于std::liststd::deque。我认为即使在普通数组中,它也会产生正确的结果。

template <typename T>
int linearSearch(const vector<T> &list, const T &key)
{
    auto itr = std::find(list.begin(), list.end(), key);
    if (itr != list.end())
        return std::distance(list.begin(), itr);
    else
        return -1;
}
int main()
{
    int intArray[] = {1, 2, 3, 4, 8, 15, 23, 31};
    std::vector<int> vec(intArray, intArray + 8);
    int i = linearSearch(vec, 15);
}

注意:C++11已启用

只要尽可能少的更改,就可以做到这一点:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Using const std::vector<T> & to prevent making a copy of the container
template <typename T>
int linearSearch(const std::vector<T> &list, int key)
{
  for (size_t i = 0; i < list.size(); i++)
  {
    if (key == list[i])
      return i;
  }
  return -1;
}
int main()
{
  std::vector<int> arr = { 1 ,2, 3, 4, 8, 15, 23, 31 } ;
  cout << "linearSearch(intArray, 3) is " << linearSearch(arr, 3) << endl;
  cout << "linearSearch(intArray, 10) is " << linearSearch(arr, 10) << endl;
  return 0;
}

我建议不要使用using namespace std;

 template <typename T>
 int linearSearch(T list, int key)

如上所述更改代码的前两行,并将arraySize替换为list.size(),对于任何类型的支持operator [](包括向量(的容器和作为连续int的索引都足够了。

请注意,当您的模板试图将数组的内容抽象为类型名T时,它隐含地假设它是key类型中的int。一个更通用的实现是:

template <typename T>
int linearSearch(T list, typename T::value_type key)

该解决方案中的另一个问题是list的通过模式。我们可以通过将其转换为这样的引用来克服这个问题:

// includes ...
#include <type_traits>
using namespace std;
template <typename T>
int linearSearch(add_lvalue_reference<T> list, typename T::value_type key){
    for (size_t i = 0; i < list.size(); i++) {
        if (key == list[i])
            return i;
    }
    return -1;
}

请看下面使用STL线性搜索算法的示例。请参阅std::find的可能实现,以及将其用于向量的示例:https://en.cppreference.com/w/cpp/algorithm/find这会给你的问题一个很好的答案。

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> intsCollection {1, 2, 3, 4, 8, 15, 23, 31};
        
    std::vector<int>::iterator val1 = std::find(intsCollection.begin(), intsCollection.end(), 3);
    int pos1 = (val1 != intsCollection.end()) ? (val1 - intsCollection.begin()) : -1;
    
    std::vector<int>::iterator val2 = std::find(intsCollection.begin(), intsCollection.end(), 10);
    int pos2 = (val2 != intsCollection.end()) ? (val2 - intsCollection.begin()) : -1;
        
    std::cout << "linearSearch(intsCollection, 3, 8) is " << pos1 << std::endl;
    std::cout << "linearSearch(intsCollection, 10, 8) is " << pos2 << std::endl;
}