Split array string in c++ - c++

I am new to cpp and have a situation in which I want to split array string
I have
for( i = k = 0; i < points[1].size(); i++ )
{
cout << points[1][k];
}
Output >>
[390.826, 69.2596]
[500.324, 92.9649]
[475.391, 132.093]
[5.60519e-44, 4.62428e-44]
I want
390.826
69.2596
500.324
92.9649
475.391
132.093
5.60519e-44
4.62428e-44
Please help me.Thanks

Assuming the type of point has public members x and y:
for( i = k = 0; i < points[1].size(); i++ )
{
cout << points[1][k].x << endl;
cout << points[1][k].y << endl;
}
If the members are something else, say, X and Y (the uppercase), then use the uppercase instead (or whatever it is).
The reason why you code prints the output that way, because operator<< has been overloaded for the type of the point. Something like:
std::ostream & operator<<(std::ostream & out, const point &p)
{
return out << "[" << p.x << "," << p.y << "]\n";
}
If you can search the above definition (or something similar) somewhere in your project source code, and then can change that to this:
std::ostream & operator<<(std::ostream & out, const point &p)
{
return out << p.x << "\n" << p.y << "\n";
}
then you wouldn't need to change the code in your for loop.

This has nothing to do with string splitting, what does points[1][k] actually return (i.e. it's type). Then look at how it has implemented the stream out operator (operator<<), and you'll see how the above is printed. This should give you a clue about the two individual values (i.e. fields of that *type), and you can simply access them and print them out.

Related

Overload << operator to change " " to "\n"

I am trying to overload
<<
operator. For instance
cout << a << " " << b << " "; // I am not allowed to change this line
is given I have to print it in format
<literal_valueof_a><"\n>
<literal_valueof_b><"\n">
<"\n">
I tried to overload << operator giving string as argument but it is not working. So I guess literal
" "
is not a string. If it is not then what is it. And how to overload it?
Kindly help;
Full code
//Begin Program
// Begin -> Non - Editable
#include <iostream>
#include <string>
using namespace std;
// End -> Non -Editable
//---------------------------------------------------------------------
// Begin -> Editable (I have written )
ostream& operator << (ostream& os, const string& str) {
string s = " ";
if(str == " ") {
os << '\n';
}
else {
for(int i = 0; i < str.length(); ++i)
os << str[i];
}
return os;
}
// End -> Editable
//--------------------------------------------------------------------------
// Begin -> No-Editable
int main() {
int a, b;
double s, t;
string mr, ms;
cin >> a >> b >> s >> t ;
cin >> mr >> ms ;
cout << a << " " << b << " " ;
cout << s << " " << t << " " ;
cout << mr << " " << ms ;
return 0;
}
// End -> Non-Editable
//End Program
Inputs and outputs
Input
30 20 5.6 2.3 hello world
Output
30
20
5.6
2.3
hello
world
" " is a string-literal of length one, and thus has type const char[2]. std::string is not related.
Theoretically, you could thus overload it as:
auto& operator<<(std::ostream& os, const char (&s)[2]) {
return os << (*s == ' ' && !s[1] ? +"\n" : +s);
}
While that trumps all the other overloads, now things get really hairy. The problem is that some_ostream << " " is likely not uncommon, even in templates, and now no longer resolves to calling the standard function. Those templates now have a different definition in the affected translation-units than in non-affected ones, thus violating the one-definition-rule.
What you should do, is not try to apply a global solution to a very local problem:
Preferably, modify your code currently streaming the space-character.
Alternatively, write your own stream-buffer which translates it as you wish, into newline.
Sure this is possible, as I have tested. It should be portable since you are specifying an override of a templated function operator<<() included from <iostream>. The " " string in your code is not a std::string, but rather a C-style string (i.e. a const char *). The following definition works correctly:
ostream& operator << (ostream& os, const char *str) {
if(strcmp(str, " ") == 0) {
os << '\n';
} else {
// Call the standard library implementation
operator<< < std::char_traits<char> > (os, str);
}
return os;
}
Note that the space after std::char_traits<char> is necessary only if you are pre-c++11.
Edit 1
I agree with Deduplicator that this is a potentially dangerous solution as it may cause undesirable consequences elsewhere in the code base. If it is needed only in the current file, you could make it a static function (by putting it within an unnamed namespace). Perhaps if you shared more about the specifics of your problem, we could come up with a cleaner solution for you.
You might want to go with a user defined literal, e.g.
struct NewLine {};
std::ostream& operator << (std::ostream& os, NewLine)
{
return os << "\n";
}
NewLine operator ""_nl(const char*, std::size_t) // "nl" for newline
{
return {};
}
This can be used as follows.
int main(int, char **)
{
std::cout << 42 << ""_nl << "43" << ""_nl;
return 0;
}
Note three things here:
You can pass any string literal followed by the literal identifier, ""_nl does the same thing as " "_nl or "hello, world"_nl. You can change this by adjusting the function returning the NewLine object.
This solution is more of an awkward and confusing hack. The only real use case I can imagine is pertaining the option to easily change the behavior at a later point in time.
When doing something non-standard, it's best to make that obvious and explicit - here, the user defined literal indeed shines, because << ""_nl is more likely to catch readers' attention than << " ".

c++ polynomial copy-constructor and ostream override cause memeory leaks?!

Hi there I am working on a polynomial class in c++. So far everything works very well. But now I encountered an error I simply cannot spot :/
polynomial.cpp
// copy-constructor
Polynomial::Polynomial(const Polynomial &p){
size = p.size;
buffer = p.buffer;
poly = new double(buffer);
for (int i = 0; i < size; ++i) poly[i] = p[i];
for (int i = size; i < buffer; ++i) poly[i] = 0;
}
// output stream override | it's a non-member function
ostream& operator<<(ostream& os, const v1::Polynomial& p){
int degree = p.degree();
stringstream ss;
if (degree == 0) ss << '0';
else if (degree > 0){
ss << '(';
for (int i = degree; i >= 0; --i){
ss << p[i];
ss << "x^";
ss << i;
if (i > 0)
ss << " + ";
}
ss << ')' << endl;
}
os << ss.str();
return os;
}
So this is how I call the copy-constructor:
// note: printing 'a' itself does not cause problems...
v1::Polynomial b(a);
cout << "Polynomial b: " << b << " degree: " << b.degree() << endl;;
The stack-log from Visual Studio says it's in Line 23 (here: the line above this one, where I actually want to print 'b') and then it continues to call some heap functions etc..
Running the program without debug (via cmd) results in an APPCRASH, with "Polynomial b: " being the last thing displayed.
I unfortunately don't know how to debug in Visual Studio, I was used to valgrind in linux, which i currently don't have set up :/
Anyone any idea? Or do you need additional info?
Regardless, many thanks in advance =)
poly = new double(buffer);
Here you allocate a single double and set it to buffer. You probably mean
poly = new double[buffer];
or whatever size you desire.
A much better solution would be using std::vector instead of raw arrays. You can copy it with =, resize it with std::vector::resize and reserve more space with std::vector::reserve.

Operator Overloading = Negation not working as intended

So I'm working with operator overloading and just realized that my negation operator isn't working as it should be. I'm not exactly sure what I've done wrong.
The .h signature
Vector & Vector::operator-()
The .cpp implementation
Vector & Vector::operator-()
{
pVec[0] = -pVec[0];
pVec[1] = -pVec[1];
pVec[2] = -pVec[2];
return *this;
};
Calling:
cout << "-Vector E = " << -VecE << (-VecE).Magnitude() << endl << endl;
The variables in VecE are like [0, 1 , 1] which means when this is called it should display them as [0, -1, -1] but it's not. So what am I missing?
EDIT: Adding copy constructor and iostream<< overload code:
Vector::Vector(const Vector & Copy)
{
pVec = new double[3];
if (0 == pVec)
{
exit(1);
}
else
{
pVec[0] = Copy.pVec[0];
pVec[1] = Copy.pVec[1];
pVec[2] = Copy.pVec[2];
}
};
ostream & operator<<(ostream & Out, Vector & RHS)
{
cout.precision(1);
Out << fixed << "[ " << RHS.pVec[0] << " " << RHS.pVec[1] << " " << RHS.pVec[2] << " ]" << resetiosflags (ios_base::fixed);
return Out;
};
You need to return a copy of the vector. The way this is written, the expression -VecE will actually modify VecE! Since you evaluate -VecE twice, you are negating the vector twice, and (of course) the negation of the negation is the original value.
To implement this change, you need to alter the operator-() declaration to return a Vector instead of a Vector &.
For example:
Vector Vector::operator-()
{
Vector copy(*this);
copy.pVec[0] = -copy.pVec[0];
copy.pVec[1] = -copy.pVec[1];
copy.pVec[2] = -copy.pVec[2];
return copy;
};
cdhowie is right. You are negating twice.
That said, I don't think you need to change the implementation.
Vector const NegVecE = -VecE;
cout << "-Vector E = " << NegVecE << NegVecE.Magnitude() << endl << endl;
EDIT: As PiotrNycz notes, though this will work, the end state is un-intuitive and therefore the correct solution is to return a copy.
{
int i = 3;
int j = -i; //you would expect i to still be 3 here
}

Difficulty in ostream

ostream& operator<<(ostream& os, const PT& p)
{
os << "(" << p.x << "," << p.y << ")";
}
PT is a structure and x , y are its members.
Can someone please explain what exactly the above line does. Can't the desired text be printed using cout?
I came across this snippet of code from this site.
It's a custom overload for operator<<.
It means you can do this:
PT p = ...;
std::cout << p << "\n";
or this:
PT p = ...;
std::stringstream ss;
ss << p << "\n";
std::cout << ss;
or lots of other useful stuff.
However, it should be noted that the code you quoted won't work properly. It needs to return os.
This provides a method of outputting the PT. Now, you can use this:
PT p;
std::cout << p;
This gets translated into a call of
operator<< (std::cout, p);
That matches your overload, so it works, printing the x and y values in brackets with less effort on the user's part. In fact, it doesn't have to be cout. It can be anything that "is" a std::ostream. There are quite a few things that inherit from it, meaning they are std::ostreams as well, and so this works with them too. std::ofstream, for file I/O, is one example.
One thing that the sample you found doesn't do, but should, though, is return os;. Without doing that, you can't say std::cout << p << '\n'; because the result of printing p will not return cout for you to use to print the newline.
It allows the << operator to append the PT object to the stream. It seems the object has elements x and y that are added with a comma separator.
This operator << overloading for outputing object of PT class .
Here:
ostream& operator<<(ostream& os, const PT& p)
First param is for output stream where p will be appended.
It returns reference to os for chaining like this:
cout << pt << " it was pt" << endl;

Track/Display Array Index As Part Of Cout (C++)

I have a command line C++ program that lets you enter basic information about a person (ID number, name, age, etc.) and I want to output to a console in the following manner:
-------------------------------------------------------------------
Index ID # First Name Last Name Age
-------------------------------------------------------------------
0 1234 John Smith 25
The person objects are stored in an array of Persons and I've overload the ostream (<<) operator to print out all of the fields like you see. The dashed lines and header come from a displayHdg() function. Anyhow, I have not been able to figure out how to get the proper index value for the array. Ideally, I'd like to generate the indices for each line, but all my attempts have failed. The array is looped through and each object printed in the main() function, and the ostream is overloaded in a person class, so I tried to use global variables as well as static variables, and all of those produce incorrect numbering (i.e. show 0, 1 the first time (for 2 objects), then change to 1, 2 on the next display). Any ideas?
Wouldn't this work? (formatting of ID field ommitted)
vector<Person> v;
for (int i = 0; i < v.size(); ++i)
cout << i + 1 << v[i] << endl;
This starts indexing at 1.
EDIT:
OK now I see what you want. You want to find an element in the vector!
std::vector<person>::iterator p =
std::find(Persons.begin(), Persons.end(), element);
if( p != Persons.end() )
{
std::cout << "index of element is: " << p-Persons.begin();
}
If you have the correct formating, you should be able to do the following:
for(size_t i = 0; i < Persons.size(); ++i)
{
cout << i << '\t' << Persons[i] << endl;
}
I would recommend taking a look at the formatting facilities in brief in this post. Using setw, left, right... manipulators is better than doing it manually.
You need to use "find" algorithms to find exact index of Person object in vector < Person>.
You could use wrapper class to hold index and print it according to your formatting in operator<<:
// wrapper to hold index
template<typename T>
struct Ti
{
Ti( size_t index, const T& t ) : index(index), val(t) {}
size_t index;
const T& val;
};
// you class
struct X
{
friend ostream& operator<<( ostream& out, Ti<X>& t );
protected:
int some_data;
};
// operator<< for X
ostream& operator<<( ostream& out, Ti<X>& t )
{
out << "test " << t.index << " " << t.val.some_data;
return out;
}
int main()
{
vector<X> xxx;
for ( size_t i =0; i < xxx.size(); ++i)
cout << Ti<X>(i+1, xxx[i]) << endl;
}