Today, I arrived at a situation, where I have a vector of tuples, where the tuples might contain several entries. Now I wanted to convert my vector of tuples to a vector of objects, such that the entries of the tuples will exactly match the uniform initialization of my object.
The following code does the job for me, but it is a bit clumsy. I'm asking myself if it might be possible to derive a generic solution that can construct …
我有一个整数向量:
std::vector<int> values = {1,2,3,4,5,6,7,8,9,10};
Run Code Online (Sandbox Code Playgroud)
鉴于这values.size()将永远是均匀的。
我只是想将相邻元素转换为一对,如下所示:
std::vector<std::pair<int,int>> values = { {1,2}, {3,4} , {5,6}, {7,8} ,{9,10} };
Run Code Online (Sandbox Code Playgroud)
即,两个相邻的元件连接成一对。
我可以使用什么STL算法来轻松实现这一点?是否可以通过一些标准算法来实现这一点?
当然,我可以轻松地编写一个老式的索引for循环来实现这一点。但我想知道使用基于范围的for循环或任何其他 STL 算法(例如std::transform等)最简单的解决方案是什么样的。
我想复制N个元素.
template< class InputIt, class Size, class OutputIt>
OutputIt myCopy_n(InputIt first, InputIt last, Size count, OutputIt result)
{
Size c = count;
while (first != last && c > 0) {
*result++ = *first++;
--c;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用std函数做到这一点?我还可以:
template< class InputIt, class Size, class OutputIt>
OutputIt myCopy_n(InputIt first, InputIt last, Size count, OutputIt result)
{
if(std::distance(first, last) > count)
return std::copy_n(first,count,result);
return std::copy(first,last,result);
}
Run Code Online (Sandbox Code Playgroud)
然而,除了麻烦之外,它超过了范围两次(距离,复制).如果我正在使用变换迭代器或过滤迭代器,那么这些是对我的过滤器/变换函数的O(N)个不必要的调用.
template <class InputIt, class OutputIt>
OutputIt copy_n_max(InputIt begin, InputIt end, OutputIt last, size_t …Run Code Online (Sandbox Code Playgroud) 我有一个整数数组,我需要删除重复项,同时保持每个整数第一次出现的顺序.我可以看到这样做,但想象有一种更好的方法可以更好地利用STL算法吗?插入不受我的控制,因此在插入之前我无法检查重复项.
int unsortedRemoveDuplicates(std::vector<int> &numbers) {
std::set<int> uniqueNumbers;
std::vector<int>::iterator allItr = numbers.begin();
std::vector<int>::iterator unique = allItr;
std::vector<int>::iterator endItr = numbers.end();
for (; allItr != endItr; ++allItr) {
const bool isUnique = uniqueNumbers.insert(*allItr).second;
if (isUnique) {
*unique = *allItr;
++unique;
}
}
const int duplicates = endItr - unique;
numbers.erase(unique, endItr);
return duplicates;
}
Run Code Online (Sandbox Code Playgroud)
如何使用STL算法完成?
当在两个连续范围上执行时,复杂性和结果之间有什么区别,std::merge并且std::inplace_merge元素都是不同的?(我不是母语为英语的人,我不确定清楚地了解"inplace"意味着什么)
问候,
我试图使用以下2个缩写的代码行(从下面的完整测试应用程序)执行从一个向量(vec1)到另一个向量(vec2)的副本:
vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), vec2.begin());
Run Code Online (Sandbox Code Playgroud)
虽然对vec2的调用设置了向量vec2的容量,但是将数据复制到vec2似乎没有填写从vec1到vec2的值.
用push_back()调用替换copy()函数按预期工作.
我在这里错过了什么?
谢谢你的帮助.然后是结果输出的vectest.cpp测试程序.
编译器:关于cygwin的gcc 3.4.4.
/**
* vectest.cpp
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec1;
vector<int> vec2;
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
vec1.push_back(4);
vec1.push_back(5);
vec1.push_back(6);
vec1.push_back(7);
vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), vec2.begin());
cout << "vec1.size() = " << vec1.size() << endl;
cout << "vec1.capacity() = " << vec1.capacity() << endl;
cout << "vec1: ";
for( vector<int>::const_iterator iter = vec1.begin(); iter < vec1.end(); ++iter ) {
cout …Run Code Online (Sandbox Code Playgroud) std::equal()是不安全的,因为该函数无法知道它是否会超出要比较的第二个容器的长度.那是:
std::vector< int > v( 100 );
std::vector< int > w( 10 );
bool same = std::equal( v.begin(), v.end(), w.begin() );
Run Code Online (Sandbox Code Playgroud)
...将导致缓冲区溢出w.
当然,我们可以测试这些东西(v.size() == w.size()),但像Visual Studio 2010这样的编译器仍然将函数本身报告为不安全.事实上,它在某种基本意义上是不安全的:具有不同经验水平的程序员团队最终会忘记比较大小.
安全的替代方案易于实施.
template< typename Iter1, typename Iter2 >
bool equal_safe( Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2 )
{
while( begin1 != end1 && begin2 != end2 )
{
if( *begin1 != *begin2 )
{
return false;
}
++begin1;
++begin2;
}
return begin1 == end1 && …Run Code Online (Sandbox Code Playgroud) 我想知道为什么STL不会重载它们的算法函数,这样我就可以通过简单地提供一个容器而不是采用更冗长的方式来传递begin + end迭代器来调用它们.我当然理解为什么我们也想使用迭代器对来处理容器/数组的子序列,但是,几乎所有对这些方法的调用都使用整个容器:
std::for_each(myVector.begin(), myVector.end(), doSomething);
Run Code Online (Sandbox Code Playgroud)
我发现它只是更方便,可读和可维护
std::for_each(myVector, doSomething);
Run Code Online (Sandbox Code Playgroud)
有没有理由STL不提供这些重载?[编辑:我的意思并不是要取代这个接口的限制之一,但要还提供了基于容器的iterface!]难道他们介绍歧义?我在考虑这样的事情:
template<typename _Container, typename _Funct>
inline _Funct for_each(_Container c, _Funct f) {
return for_each(begin(c), end(c), f);
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我想知道为什么仿algorithm函数通过副本传递给函数:
template <typename T> struct summatory
{
summatory() : result(T()) {}
void operator()(const T& value)
{ result += value; std::cout << value << "; ";};
T result;
};
std::array<int, 10> a {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }};
summatory<int> sum;
std::cout << "\nThe summation of: ";
std::for_each(a.begin(), a.end(), sum);
std::cout << "is: " << sum.result;
Run Code Online (Sandbox Code Playgroud)
我期待以下输出:
总和:1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 是:143
但是sum.result包含0,这是在ctor中分配的默认值.实现所需行为的唯一方法是捕获以下内容的返回值for_each …
考虑以下课程:
struct C
{
/* Class contents, without any arithmetic operator... */
constexpr operator int() noexcept; // Implicit conversion to int
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:
std::sort当前使用默认<运算符的标准算法?LessThanComparable概念吗?LessThanComparable.c++ ×10
stl-algorithm ×10
stl ×6
c++11 ×3
algorithm ×1
c++-concepts ×1
c++17 ×1
containers ×1
duplicates ×1
overloading ×1
stdset ×1
stdtuple ×1
stdvector ×1
vector ×1