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

时间:2013-03-22 19:29:14

标签: c++ arrays vector stl

此代码是使用数组的线性搜索程序。出于好奇,我想知道如何使用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;
}

6 个答案:

答案 0 :(得分:2)

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已启用

答案 1 :(得分:2)

您可以通过更改参数类型并在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;
}

答案 2 :(得分:1)

尽可能少的更改你可以这样做:

#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;

答案 3 :(得分:1)

这可行(它基于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。我认为即使在正常的阵列中它也会产生正确的结果。

答案 4 :(得分:1)

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

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

请注意,虽然您的模板尝试将数组的内容抽象为类型名T,但它隐含地假定它是int类型中的key。更通用的实现是:

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;
}

答案 5 :(得分:0)

您可能要求这样的东西(使用std :: vector而不是手工制作的类):

const size_t count = 8; 
int values[count] = {1, 2, 3, 4, 8, 15, 23, 31};
std::vector<int> intArray;
intArray.assign(values, values + count);

std::vector<int>::iterator val1 = std::find(intArray.begin(), intArray.end(), 3);
int pos1 = (val1 != intArray.end()) ? (val1 - intArray.begin()) : -1;

std::vector<int>::iterator val2 = std::find(intArray.begin(), intArray.end(), 10);
int pos2 = (val2 != intArray.end()) ? (val2 - intArray.begin()) : -1;

cout << "linearSearch(intArray, 3, 8) is " << pos1 << endl;
cout << "linearSearch(intArray, 10, 8) is " << pos2 << endl;