向量值(字符串和整数)求和C++
Vector Values (String and Int) Summation C++
我已经从Python转移到C++,只是想知道对向量(列表(对象所包含的值求和的方法。例如,在python中,我可以使用以下代码:
totalSum = 0
myList = [1,2,3,4,5]
for i in myList:
totalSum += i
print(totalSum)
// Output:
15
但是,我想学习在C++中执行此操作的方法
totalSum = ""
myList = ["Hello", "World!"]
for i in myList:
totalSum += i
totalSum += " "
print(totalSum)
//Output:
Hello World!
这个是字符串组合。
您能否提供如何在 c++ 中执行此操作?
我已经C++尝试了下面的代码进行测试,但是,它没有成功编译:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// A random list/vector here:
vector <double> v = {1, 2, 3, 4, 5};
// Declaring the final string to gather all the vector values:
int sum;
// Iterating through the vector
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
cout << sum;
return 0;
}
您的代码工作正常,除了您尚未初始化 sum 变量的事实。
以下是一些不言自明的代码,讨论您可以使用什么(基于对您的问题的评论(:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
int main() {
// For strings:
std::string str;
std::vector<std::string> v = {"Hello", "World!"};
// Method 1: Using range-based for loop:
for (auto &&i : v) {
str += i;
str += " ";
}
std::cout << str << std::endl;
// Method 2: Using std::accumulate():
str = std::accumulate(v.begin(), v.end(), std::string(), [](std::string a, std::string b) {
return std::move(a) + b + " ";
});
std::cout << str << std::endl;
// Method 3: The usual for-loop:
str = "";
for (size_t i = 0; i < v.size(); ++i) {
str += v.at(i); // str += v[i];
str += " ";
}
std::cout << str << std::endl;
// Method 4: Using iterators:
str = "";
for (auto i = v.begin(); i < v.end(); ++i) { // for (auto i = std::begin(v); i < std::end(v); std::advance(i))
str += *i;
str += " ";
}
std::cout << str << std::endl;
// For numbers:
std::vector<int> v2 = {1, 2, 3, 4, 5};
int sum = 0;
// Method 1: Using range-based for loop:
for (auto &&i : v2)
sum += i;
std::cout << sum << std::endl;
// Method 2: Using std::accumulate():
sum = std::accumulate(v2.begin(), v2.end(), 0);
std::cout << sum << std::endl;
// Method 3: The usual for-loop:
sum = 0;
for (size_t i = 0; i < v2.size(); ++i)
sum += v2.at(i); // sum += v2[i]
std::cout << sum << std::endl;
// Method 4: Using iterators:
sum = 0;
for (auto i = v2.begin(); i < v2.end(); ++i) // for (auto i = std::begin(v2); i < std::end(v2); std::advance(i))
sum += *i;
std::cout << sum << std::endl;
return 0;
}
如果您使用的是 C++14 或更高版本,则可以替换从(std::string a, std::string b)
传递到std::accumulate
到(auto a, auto b)
的 lamda 的参数列表。
如果您使用的是std::begin()
或std::end()
或std::advance()
,则需要包含<iterator>
。如果您不使用<numeric>
,也可以删除std::accumulate()
。
有关您在我的代码中看到的任何不熟悉内容的文档,请访问 https://en.cppreference.com/。
程序有错误。在for
循环中,您尝试将整数与v.size()
返回的unsigned long long int
进行比较(在编译器参数中使用-Wall
模式来获取它(。
对于每种语法,方法定义如下:
#include <iostream>
#include <vector>
int main(void) {
std::vector <std::string> v = {"Hello", "World"};
std::string sum;
for (auto i : v) {
sum += i;
sum += ' ';
}
std::cout << sum << std::endl;
return 0;
}
这将打印:
Hello World
如果你对 STL 算法感兴趣,你可以通过使用 std::accumulate 来实现这一点:
#include <vector>
#include <numeric>
#include <string>
#include <iostream>
int main()
{
std::vector <double> v = {1, 2, 3, 4, 5};
std::cout << std::accumulate(v.begin(), v.end(), 0.0) << "n";
std::vector<std::string> s = {"Hello", "World", "abc", "123"};
std::cout << std::accumulate(s.begin(), s.end(), std::string(),
[](auto& total, auto& str) { return total + str + " "; });
}
输出:
15
Hello World abc 123
相关文章:
- 求和 int 乘以双倍时的欠/过近似
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- 递归求和任务的错误答案
- 向量值(字符串和整数)求和C++
- 通过 RCPP 中的索引求和
- 矩阵行求和与 RowMajor 和 ColMajor 数据排列的奇怪性能差异
- 使用192/256位整数求和无符号64位整数向量的点积的最快方法
- 如何使用循环对所有数字求和?
- 循环中的线程 Runnin 用于对 c++ 中的数字求和
- 使用动态数组求和和和求平均值
- 找到所有与自己求和的数字X的快速方法,去掉一个数字得到N
- 如何对堆栈中的所有项目求和?Postfix计算器
- MPI 归约操作中的求和顺序
- 在高通六边形处理器的word32上执行水平求和的最快方法是什么
- 模板化求和(参数...)可变参数函数无法编译
- 平衡数组索引,同时从左和右对数组求和
- g++ 6.3,avx 内联函数上的 Kahan 求和用易失性关键字进行序列化
- 简单的计数和求和函数没有按照我预期的方式工作
- C++ 如何从文本文件中扫描二维数组并对行求和?
- C++ 犰狳和OpenMp:外积求和的并行化 - 定义犰狳矩阵的约简