gpt4 book ai didi

c++ - 将值从 vector 存储为字符串作为逗号分隔值

转载 作者:行者123 更新时间:2023-11-30 04:34:41 30 4
gpt4 key购买 nike

如何将函数返回的值作为逗号分隔值存储到字符串中。谁能帮帮我……?

const myVector &vecList = obj.get_List();
vector<myNumVector *>::const_iterator iter;
for (iter= vecList.begin(); iter!= vecList.end(); iter++)
{
string myNum = (*iter)->get_myNum();
string myNumList = ?
//myNumList should be = drt123,ret34,dfghgd234.... if these are the return values
} //can we achive this by use of some sting functions..?

最佳答案

从我发布的链接可以看出,有很多方法可以做到这一点。我相信这是最简单的:

#include <vector>
using std::vector;

#include <string>
using std::string;

#include <boost/assign/list_of.hpp>
using boost::assign::list_of;
namespace ba = boost::assign;

vector<string> victor = list_of
("Clarence Oveur")
("Roger Murdock")
("Victor Basta");

int main() {
string result;
for(vector<string>::iterator it = victor.begin();
it != victor.end();
++it) {
if(it != victor.begin()) {
result += ", ";
}
result += *it;
}
cout << result << "\n";
}


编辑:直接翻译成 OP 的问题:

const myVector &vecList = obj.get_List();
vector<myNumVector *>::const_iterator iter;
string myNumlist;
for (iter= vecList.begin(); iter!= vecList.end(); iter++)
{
string myNum = (*iter)->get_myNum();
if(iter!=vecList.begin()) {
nyNumList += ",";
}
myNumList += myNum;
}


编辑:通过从以前的解决方案中删除 bool first 来简化。

关于c++ - 将值从 vector 存储为字符串作为逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833150/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com