如何使用STL将数组转换为矢量
How to convert arrays to vectors using 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;
}
您可以通过更改参数类型和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::list
和std::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;
}
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何在C++中将数组转换为矢量指针
- 将矢量<T>转换为矢量<数组<T>>
- JSON转换为nlohmann JSON-lib中的结构数组
- 将C++中的数组转换为字符串
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 将 int 数组转换为 std::vector<int*>
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 将链表转换为指针数组时出错
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将浮点数转换为无符号字符数组并打印出来
- 创建一个函数来转换数组元素的类型并返回数组的地址
- 在 c++ 中动态强制转换 ** 数组
- 返回并强制转换数组指针(错误:数组下标的类型..无效)
- 用于递归模板求值的强制转换数组类型
- node.js中使用node-gyp的转换数组
- 如何转换数组中的参数列表