Sign in to follow this  

cannot convert const char to string

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok im trying to do somthing like this string *szFiles[] = { "0.bmp","1.bmp","2.bmp", "3.bmp","4.bmp","5.bmp", "6.bmp","7.bmp","8.bmp","9.bmp", "10.bmp","11.bmp","12.bmp", "13.bmp","14.bmp","15.bmp", "16.bmp","17.bmp","18.bmp", "19.bmp","20.bmp","21.bmp", "22.bmp","23.bmp","24.bmp", "25.bmp","26.bmp","27.bmp", "28.bmp","29.bmp","30.bmp", "31.bmp","32.bmp","33.bmp", "34.bmp","35.bmp","36.bmp", "37.bmp","38.bmp","39.bmp", "40.bmp","41.bmp","42.bmp", "43.bmp","44.bmp","45.bmp", "46.bmp","47.bmp","48.bmp", "49.bmp","50.bmp","51.bmp", "52.bmp","53.bmp","54.bmp", "55.bmp","56.bmp","57.bmp", "58.bmp","59.bmp","60.bmp", "60.bmp","60.bmp","64.bmp"}; what do i need to do so that the compiler will read those as strings not const char*

Share this post


Link to post
Share on other sites
You'll love this:



#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main( int argc, char **argv ) {

std::stringstream ss;
std::string str;
std::vector< std::string > v;

for (int i = 0; i < 20; i++) {

// glue the loop number into a string with ".bmp" on the end
ss.clear();
ss << i << ".bmp";

// shove it into a std::string
str.clear();
ss >> str;

// push the new string onto the vector
v.push_back( str );

}

for (unsigned int i = 0; i < v.size(); i++) {
std::cout << v[i] << std::endl;
}

return 0;
}




Outputs:

0.bmp
1.bmp
2.bmp
3.bmp
4.bmp
5.bmp
6.bmp
7.bmp
8.bmp
9.bmp
10.bmp
11.bmp
12.bmp
13.bmp
14.bmp
15.bmp
16.bmp
17.bmp
18.bmp
19.bmp

Share this post


Link to post
Share on other sites
Quote:
Original post by leiavoia
You'll love this:

*** Source Snippet Removed ***
You'll love this even more:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main(int argc, char *argv[])
{
using namespace std;
vector<int> V(20);
for(int i = 0; i < 20; ++i)
V[i] = i;
copy(V.begin(), V.end(), ostream_iterator<int>(cout, ".bmp\n"));
return 0;
}


Same output.

Share this post


Link to post
Share on other sites
Quote:
Original post by leiavoia
Blast you Oluseyi!

Good job really. made me go look up some things ;-) But i think the OP wanted a list of strings, not just the output.

stringstream ss;
copy(V.begin(), V.end(), ostream_iterator<int>(ss, ".bmp\n"));

...

<for each line>
getline(ss, line);
<use line>

Share this post


Link to post
Share on other sites

#include <cstddef>
#include <iterator>
#include <functional>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>

struct gen_name : std::unary_function< std::string&, std::string& > {

size_t n;

gen_name(): n(0) {}

std::string& operator()(std::string& s) {
std::ostringstream oss;
oss << n, ++n;
return s.insert(0, oss.str());
}

};

int main() {

std::vector<std::string> vec_of_strings(65, ".bmp");

std::transform(vec_of_strings.begin(), vec_of_strings.end(),
vec_of_strings.begin(), gen_name());

std::copy(vec_of_strings.begin(), vec_of_strings.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));

return 0;

}

Share this post


Link to post
Share on other sites
If you're lucky enough to be using an STL with SGI extensions:

int main()
{
vector<int> v(65);
iota(v.begin(), v.end(), 0);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, ".bmp\n"));
}

Share this post


Link to post
Share on other sites
here is a more efficent version than the one i previously posted

#include <cstddef>
#include <iterator>
#include <functional>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>

struct gen_name : std::unary_function< void, std::string > {

size_t n;

gen_name(): n(0) {}

std::string operator()() {
std::ostringstream oss;
oss << n, ++n;
return oss.str();
}

};

int main() {

std::vector<std::string> vec_of_strings;

std::generate_n(std::back_inserter(vec_of_strings), 65, gen_name());

std::transform(vec_of_strings.begin(), vec_of_strings.end(), vec_of_strings.begin(),
std::bind2nd(std::mem_fun_ref((std::string& (std::string::*)(const char*))(&std::string::append)), ".bmp"));

std::copy(vec_of_strings.begin(), vec_of_strings.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));

return 0;

}

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
If you're lucky enough to be using an STL with SGI extensions:
*** Source Snippet Removed ***
Yes. The fact that iota is an SGI extension is why I had to go through the cumbersome for loop routine, the alternative to which was writing a function object that returned sequential elements per invocation, used in conjunction with std::generate or std::generate_n. Something like this:

struct gen
{
private:
int val;

public:
gen(int n = 0) : val(n) {}

int operator() ()
{
return val++;
}
};

int main(int argc, char *argv[])
{
using namespace std;
vector<int> V(20);
generate(V.begin(), V.end(), gen());

...
}



iota should be standard.

Share this post


Link to post
Share on other sites
iota's fun, but I kind of feel like an equivalent generator function should be standard instead... something more or less exactly like the generator you posted, but templated and with the start value passed into the constructor. That way iota would be rolled into generate, generate_n would simulate a (nonexistent) iota_n, and this thing could be done in one line. [grin]
generate_n(ostream_iterator<int>(cout, ".bmp\n"), 65, iota_gen<int>(0));


[Edited by - Sneftel on October 8, 2004 6:16:58 PM]

Share this post


Link to post
Share on other sites

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this