shaolinfunk
asked on
How do I print to a CString?
I have a vector of integers defined by vector <int> vIntegers.
Values were put into the vector using the .push_back method.
How do I get the integers from the vector to a CString such that the CString would look something like:
12, 31, 42, 53, 64
notice that the integers are separated by commas, and I would like to use the FIFO method such that the value first pushed into the vector is the first value to appear in the CString....
Values were put into the vector using the .push_back method.
How do I get the integers from the vector to a CString such that the CString would look something like:
12, 31, 42, 53, 64
notice that the integers are separated by commas, and I would like to use the FIFO method such that the value first pushed into the vector is the first value to appear in the CString....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The attached code does not need the MFC.
Output:
0 1 2 3 4 5 6 7 8 9
Output:
0 1 2 3 4 5 6 7 8 9
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector <int> v(&array[0], &array[10]);
std::stringstream out;
for (int i = 0; i < 10; ++i)
{
out << i << " ";
}
std::string s = out.str();
std::cout << s << "\n";
return 0;
}
Sorry.
Code in my previous comment is wrong.
Code in my previous comment is wrong.
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector <int> v(&array[0], &array[9]);
std::stringstream out;
for (int i = 0; i < 10; ++i)
{
out << v[i] << " ";
}
std::string s = out.str();
std::cout << s << "\n";
return 0;
}
I think the standard way to convert such vector into a string is:
std::stringstream out;
std::copy(v.begin(), v.end(), std::ostream_iterator(out, " "));
std::stringstream out;
std::copy(v.begin(), v.end(), std::ostream_iterator(out,
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector <int> v(&array[0], &array[9]);
std::stringstream out;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(out, " "));
std::string s = out.str();
std::cout << s << "\n";
return 0;
}
ASKER
will test this out on Monday folks. enjoy your weekend.
ASKER
zoppo, your solution is the shortest and most compact. is there a way to insert a carriage return so that instead of 1,2,3,4,5,6..
i get instead:
1,
2,
3,
4,
5,
6?
i get instead:
1,
2,
3,
4,
5,
6?
Yes, of course. But there's one thing you have to take care: Depending on how you want to use the string a linebreak is done with '\n' (i.e. for writing string into a ASCII text file) or '\r\n' (i.e. for displaying the string in a multiline text box).
Simply replace the 'AppendFormat' statement with one of these:
>result.AppendFormat("%d,\ n", v[i]);
or
>result.AppendFormat("%d,\ r\n", v[i]);
If you want to learn more about using formatting strings this way take a look here: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
The format string syntax for CString::AppendFormat and CString::Format are the same as shown there ...
Simply replace the 'AppendFormat' statement with one of these:
>result.AppendFormat("%d,\
or
>result.AppendFormat("%d,\
If you want to learn more about using formatting strings this way take a look here: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
The format string syntax for CString::AppendFormat and CString::Format are the same as shown there ...
//vector v;
CString result;
CString tmp;
for (int i = 0; i < v.size(); ++i)
{
tmp.Format("%d, ", v[i]);
result += tmp;
}
// now result contains the integers from the vector.