1

how can I store the values returned from a function to a string as comma seperated values. Can anyone help me..?

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..?
Nim
  • 33,299
  • 2
  • 62
  • 101
vidhya
  • 2,861
  • 5
  • 28
  • 28

3 Answers3

1

As can be seen from the links I posted, there are lots of ways to do this. Here is, I believe, the simplest:

#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";
}


EDIT: To translate directly to OP's question:
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;
}


EDIT: Simplified by removing bool first from previous solution.
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Except that this also assumes that `*it` will return the value of the function call `it->get_MyNum()`, which is at the end of the day what I think OP is having trouble with, – John Dibling Apr 29 '11 at 14:40
  • @John, you should see my [strict weak ordering](http://en.wikipedia.org/wiki/Strict_weak_ordering) example from [Airplane II](http://www.uselessmoviequotes.com/umq_a004.htm). – Robᵩ Apr 29 '11 at 14:43
  • @John, thanks pointing out the disconnect with OP's question. Is this better? – Robᵩ Apr 29 '11 at 14:49
  • I think so, we'll just have to see how OP responds since the question doesn't really clarify what exactly they are having trouble with. – John Dibling Apr 29 '11 at 14:53
1
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
    
int main () {
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);

    std::stringstream list;
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(list, ","));

    std::cout << list.str();

    return 0;
}

Outputs: 1,2,3,4,

more modern approach, also solving the trailing ","

#include <string>
#include <numeric>
#include <iostream>

int main() {
    const auto v = {1, 2, 3, 4};
    const auto list = std::accumulate(begin(v), end(v), std::string{}, [](const std::string& so_far, const auto& next) {
        return so_far + (so_far.empty() ? "" : ", ") + std::to_string(next);
    });

    std::cout << list;

    return 0;
}
Marco Kinski
  • 302
  • 3
  • 7
0

Yes, this can be achieved using string functions, along with a handful other methods.

Given a string myNumList defined outside the loop, you could simply

myNumList += "," + myNum;

although that would add an extraneous comma in the beinning, so check if iter is pointing there first:

if(iter != vecList.begin())
    myNumList += ',';
myNumList += myNum;
Cubbi
  • 46,567
  • 13
  • 103
  • 169