Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

cannot convert const char to string

This topic is 5128 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
Advertisement
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 << 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;
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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!