49 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <vector> //动态数组
#include <numeric>
#include <string>
using std::cin;
using std::endl;
using std::cout;
using std::vector;
using std::accumulate; //accumulate()函数返回数组内元素的累积值
using std::string;
int main(int argc, const char* argv[]) {
vector<string> v = { "zhang", "meng", "nan" , "1220310013"};
string str; //创建空字符串
str = accumulate(v.begin(), v.end(), str); //将v中所有字符加入str中
cout << str;
return 0;
}
#include <iostream> // 引入标准输入输出流库
#include <vector> // 引入动态数组vector容器库
#include <numeric> // 引入numeric库提供accumulate函数
#include <string> // 引入字符串库
using std::cin;
using std::endl;
using std::cout;
using std::vector;
using std::accumulate; // 引入accumulate函数用于累加容器内的元素
using std::string;
int main(int argc, const char* argv[]) {
// 初始化一个包含字符串的向量
vector<string> v = { "zhang", "meng", "nan", "1220310013" };
string str; // 创建一个空字符串,用于存储累积的结果
// 使用accumulate函数将vector v中的所有字符串连接起来
// accumulate的第三个参数是累积的初始值空字符串str用于开始累积
str = accumulate(v.begin(), v.end(), str);
// 输出累积后的字符串
cout << str; // 输出:"zhangmengnan1220310013"
return 0; // 程序结束
}