4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Moschops
0

You definitely can't use find_first_of and substr. They are members of the std::string class, and as such must be called using either the "dot" or "arrow" operator on a std::string object or pointer to std::string respectively. This means you won't even be able to get to them from a C-style string (char array).

The constructor for an istringstream has a reference to a std::string as an argument, so you may be able to create one from a char array because you can construct an std::string from a char array, but I have my doubts. Whatever you do to the stream probably won't be reflected in the array though.

The make_pair function is a templatized function, which means it can accept anything as an argument. You can use a char array as an argument.

But you want to know the best way to find out? Try it yourself. There's precious few better ways to learn than through experimentation.

Edited by Fbody: n/a

0

Hey there,i have this code. It shows no error in the console but it shows a debug error,and in the output only the first "for"

#include <iostream>
	#include <string>
	
	using namespace std;
        int main ()
	{  const int polynomsize=5;
	   string sub[polynomsize];
	   string coefficient[polynomsize];
	  string str ="2.4x^4 - 1.2x^3 + 6x^2 + 4.1x + 9.2";
	  size_t found;
 
	  found=str.find_first_of("+-");
	   
	   sub[0]=str.substr(0,found);
	  cout<<"sub1"<<"  "<<sub[0]<<endl;
	  while (found!=string::npos)
	  { for (int i=1;i<=polynomsize;i++)
	 
	         { int a=found;
           found=str.find_first_of("+-",found+1);
	           sub[i]=str.substr(a,found-a);
	           cout<<"sub"<<i+1<<"  "<<sub[i]<<endl;
	     
	  }

	     
	  }
	 
	    for (int j=0;j<=polynomsize;j++)
		{ size_t foundx;
		foundx=sub[j].find_first_of("x");
		coefficient[j]=sub[j].substr(0,foundx);
		cout<<coefficient[j];
		}
	 
	  return 0;
	}
0

I haven't read your example code as I'm in a rush, but if you ever have a char array, and you want to carry out string operations on it, you can turn it into a string, carry out your operations, and then turn it back to a char array.

#include <string>
#include <cstring>
int main()
{

char aCharArray[] = "Hello World"; 
std::string aString(aCharArray);
// Carry out string operations....
aString.insert(4,"X");
char* cstr = new char [aString.size()+1];
strcpy (cstr, aString.c_str());
std::cout << cstr;

// Ignore memory leaks, I'm not being paid enough to worry about them :p

}

Output:

HellXo World

Edited by Moschops: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.