Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用STL将数组转换为向量_C++_Arrays_Vector_Stl - Fatal编程技术网

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

C++ 如何使用STL将数组转换为向量,c++,arrays,vector,stl,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++

这段代码是一个使用数组的线性搜索程序。出于好奇,我想知道如何使用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;
}
#包括
#包括
使用名称空间std;
模板
int-linearSearch(T-list[],int-key,int-arraySize)
{
for(int i=0;i模板
int linearSearch(常量向量和列表、常量T和键)
{
自动itr=std::find(list.begin(),list.end(),key);
if(itr!=list.end())
返回std::distance(list.begin(),itr);
其他的
返回-1;
}
int main()
{
int intArray[]={1,2,3,4,8,15,23,31};
标准:向量向量向量(intArray,intArray+8);
int i=线性搜索(vec,15);
}
注意:C++11已启用

模板
int linearSearch(常量向量和列表、常量T和键)
{
自动itr=std::find(list.begin(),list.end(),key);
if(itr!=list.end())
返回std::distance(list.begin(),itr);
其他的
返回-1;
}
int main()
{
int intArray[]={1,2,3,4,8,15,23,31};
标准:向量向量向量(intArray,intArray+8);
int i=线性搜索(vec,15);
}

注意:C++11已启用

您可以通过更改参数类型和在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;
}
#包括
#包括
#包括
使用名称空间std;
模板
整数线性搜索(向量列表,整数键)
{
对于(size_t i=0;icout您可以通过更改参数类型和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;
}
#包括
#包括
#包括
使用名称空间std;
模板
整数线性搜索(向量列表,整数键)
{
对于(size_t i=0;icout通过尽可能少的更改,您可以执行以下操作:

#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;
}
#包括
#包括
#包括
使用名称空间std;
//使用const std::vector&防止复制容器
模板
int-linearSearch(常量标准::向量和列表,int键)
{
对于(size_t i=0;icout通过尽可能少的更改,您可以执行以下操作:

#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;
}
#包括
#包括
#包括
使用名称空间std;
//使用const std::vector&防止复制容器
模板
int linearSearch(const std::vector&list,int key)
{
对于(size_t i=0;i
#包括
#包括
#包括
使用名称空间std;
模板
int linearSearch(转发器beg、转发器端、类型键)
{
int i=0;
for(;beg!=结束;++beg)
{
如果(键==*beg)
返回i;
i++;
}
返回-1;
}
int main()
{
向量vec={1,2,3,4,5,6,7};
cout这可能有效(它基于STL实现):

#包括
#包括
#包括
使用名称空间std;
模板
int linearSearch(转发器beg、转发器端、类型键)
{
int i=0;
for(;beg!=结束;++beg)
{
如果(键==*beg)
返回i;
i++;
}
返回-1;
}
int main()
{
向量vec={1,2,3,4,5,6,7};
库特
此解决方案中的另一个问题是
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;
}
//包括。。。
#包括
使用名称空间std;
模板
int linearSearch(添加左值参考列表,类型名称T::值类型键){
对于(size_t i=0;i
此解决方案中的另一个问题是
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;
}
//包括。。。
#包括
使用名称空间std;
模板
int linearSearch(添加左值参考列表,类型名称T::值类型键){
对于(size_t i=0;i
您可能要求这样的类(使用std::vector代替手工制作的类):

const size\u t count=8;
int值[计数]={1,2,3,4,8,15,23,31};
std::矢量阵列;
intArray.assign(值、值+计数);
std::vector::iterator val1=std::find(intArray.begin(),intArray.end(),3);
int pos1=(val1!=intArray.end())?(val1-intArray.begin()):-1;
std::vector::iterator val2=std::find(intArray.begin(),intArray.end(),10);
int pos2=(val2!=intArray.end())?(val2-intArray.begin()):-1;

cout您可能要求这样的类(使用std::vector代替手工制作的类):

const size\u t count=8;
int值[计数]={1,2,3,4,8,15,23,31};
std::矢量阵列;
intArray.assign(值、值+计数);
std::vector::iterator val1=std::find(intArray.begin(),intArray.end(),3);
int pos1=(val1!=intArray.end())?(val1-intArray.begin()):-1;
std::vector::iterator val2=std::find(intArray.begin(),intArray.end(),10);
int pos2=(val2!=intArray.end())?(val2-intArray.begin()):-1;

你所要做的就是把
T list[]
改成
const std::vector&
int intArray[]
改成
std::vector intArray
。你不必再传递列表的大小了。实际上你也不需要
linearSearch
函数,你只需使用
std::fin即可
// 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;
}
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;