C++ 向向量的所有元素添加int
我有一个带整数的向量,比如说C++ 向向量的所有元素添加int,c++,algorithm,c++11,add,stdvector,C++,Algorithm,C++11,Add,Stdvector,我有一个带整数的向量,比如说{1,2,3,4} 如何向每个元素添加常量10,以将向量修改为{11,12,13,14} 如果我想将每个元素除以一个int并修改向量,那么除法也是一样的。我还没有找到解决办法。有很多方法可以实现你的目标,但我将公布其中两种最常用的方法 解决方案1: 您必须使用迭代器来解决根本问题。 在下面的代码中,我将遍历向量元素的所有引用并修改它们的值 代码: #include <iostream> #include <vector> using names
{1,2,3,4}
如何向每个元素添加常量10
,以将向量修改为{11,12,13,14}
如果我想将每个元素除以一个
int
并修改向量,那么除法也是一样的。我还没有找到解决办法。有很多方法可以实现你的目标,但我将公布其中两种最常用的方法
解决方案1:您必须使用迭代器来解决根本问题。
在下面的代码中,我将遍历向量元素的所有引用并修改它们的值 代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4};
for(int& x : vec) // if you want to add 10 to each element
x += 10;
for(int& x : vec) // if you want to divide each element
x /= 2;
for (int x : vec) // print results
printf("%d\n", x);
}
#include <iostream>
#include <vector>
#include <algorithm> // you need to include this to use std::for_each() !
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4};
std::for_each(vec.begin(), vec.end(), [](int &n){ n+=10; }); // add 10 to each element
std::for_each(vec.begin(), vec.end(), [](int &n){ n/=3; }); // divide each element by 3
for (int x : vec) // print results
printf("%d\n", x);
}
11 12 13 14
5 6 6 7
#包括
#包括
使用名称空间std;
int main()
{
向量向量{1,2,3,4};
for(int&x:vec)//如果要向每个元素添加10
x+=10;
for(int&x:vec)//如果要分割每个元素
x/=2;
for(int x:vec)//打印结果
printf(“%d\n”,x);
}
解决方案2:在标头算法中使用标准库(STL)提供的函数。这也归结为使用迭代器,但在这个解决方案中,您只需要写一行 代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4};
for(int& x : vec) // if you want to add 10 to each element
x += 10;
for(int& x : vec) // if you want to divide each element
x /= 2;
for (int x : vec) // print results
printf("%d\n", x);
}
#include <iostream>
#include <vector>
#include <algorithm> // you need to include this to use std::for_each() !
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4};
std::for_each(vec.begin(), vec.end(), [](int &n){ n+=10; }); // add 10 to each element
std::for_each(vec.begin(), vec.end(), [](int &n){ n/=3; }); // divide each element by 3
for (int x : vec) // print results
printf("%d\n", x);
}
11 12 13 14
5 6 6 7
#包括
#包括
#include//要使用std::for_each(),您需要包含此项!
使用名称空间std;
int main()
{
向量向量{1,2,3,4};
std::for_each(vec.begin(),vec.end(),[](int&n){n+=10;});//为每个元素添加10
std::for_each(vec.begin(),vec.end(),[](int&n){n/=3;});//将每个元素除以3
for(int x:vec)//打印结果
printf(“%d\n”,x);
}
…或者,如果您不想自己编写循环:
std::for_each(std::begin(vec), std::end(vec), [](int& x) { x += 10; });
但是循环更清晰。或者只使用for循环,例如:
for (auto& item : vec)
{
item += 10;
}
但我承认,对于每个人来说,看起来都更好…考虑使用
变换:
std::vector<int> v = { 1, 2, 3, 4 };
std::transform(
v.begin(), v.end(), // The input source
v.begin(), // The output destination
[](int x) { return x + 10; } // The transforming function
);
std::vector v={1,2,3,4};
std::transform(
v、 begin(),v.end(),//输入源
v、 begin(),//输出目标
[](int x){return x+10;}//变换函数
);
比如说,{1,2,3,4}
,我如何为每个值添加一个常量10
元素将向量修改为{11,12,13,14}
。同样
带有的事物除以如果[…]
如何使用!
std::valarray
是用于表示和操作
价值观它支持元素级数学运算和各种
广义下标运算符、切片和间接访问的形式
如果你能使用它们,那只是一种操作()
了解你知道如何迭代向量吗?如果你不知道,有一个向量没有多大意义。我试着用一行来做这件事。我使用的变换可以将一个向量的所有元素与另一个向量的元素相加。但我找不到一种方法,可以将所有元素和向量的所有元素相加成一个常量。“我还没有找到解决方案”,我觉得很难相信。您查看了哪些资源?有什么接近的吗?你试过了吗?你这样做的时候发生了什么?这是一个非常基本的操作。请发布您的完整源代码,以便我们能够帮助了解发生了什么。请查看下面的答案,并将其中一个标记为帮助功能读者的答案。