向量值(字符串和整数)求和C++

Vector Values (String and Int) Summation C++

本文关键字:求和 C++ 整数 字符串 向量      更新时间:2023-10-16

我已经从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