I am attempting to implement a std::list to replace a linked list in this assignment. I am not allowed to change the declarations and can only change code in the .cpp file. For the most part I am making progress but I am having trouble implementing this
std::ostream& operator<< (std::ostream& out, const Section& section);
namely when I try to create an iterator it fails. I've used the iterator elsewhere in the code so I don't understand why it's failing here, I believe it's because it's private but I'm not sure how to resolve the issue without changing the .h file which was explicitly prohibited:
std::ostream& operator<< (std::ostream& out, const Section& section)
{
// 1. print the section header
out << setw(8) << left << section.getCourse()
<< setw(6) << left << section.getCallNumber();
out << ": " << section.getNumberOfStudents() << " students\n";
// 2. collect the students, sort, and print
Student* students = new Student[section.getNumberOfStudents()];
{
int i = 0;
for ( auto pos = section.students.begin();
pos != section.students.end(); pos++)
{
students[i] = pos;
++i;
}
}
sort (students, students+section.getNumberOfStudents());
for (int i = 0; i < section.getNumberOfStudents(); ++i)
out << " " << students[i] << "\n";
out << flush;
return out;
}
students[i] = pos;
should be changed to
students[i] = *pos;
because you want to copy the Student the iterator references, not the iterator itself.
But why a dynamic array of Student rather than a std::vector<Student>? Currently you have a memory leak because you don't delete[] students;
Edit 1
Removed.
Edit 2
Other than that, all I can see that it wrong is a missing std:: in front of
sort (students, students+section.getNumberOfStudents());
this is assuming there is no custom sort method being used.
Edit 3
Going off the rails here:
students[i] = *pos;
copies a Student from the list into the dynamic array students. This could be expensive, so here is an alternative:
First the bits and pieces needed to prove this out: Required includes
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
a minimal Student class
class Student
{
std::string name;
public:
Student(std::string inname):name(inname)
{
}
const std::string & getname() const
{
return name;
}
friend bool operator<(const Student & a, const Student &b)
{
return a.name < b.name;
}
};
a minimal Section class
class Section
{
public:
std::list<Student> students;
};
a minimal outstream operator
std::ostream& operator<<(std::ostream& out, const Section& section)
{
A std::vector instead of an array, and a vector of constant references so we don't have to copy the students.
std::vector<std::reference_wrapper<const Student>> students;
Store references in the vector. Probably could do a one liner with std::copy and std::back_inserter, but this is getting a bit too much to absorb for one example.
for (const auto & student: section.students)
{
students.push_back(std::ref(student));
}
Sort the vector
std::sort(students.begin(), students.end());
print the vector
for (const auto & student: students)
{
out << student.get().getname() << " ";
}
return out;
}
and one main to rule them all and in the darkness bind them
int main()
{
Section s;
s.students.emplace_front("Tom");
s.students.emplace_front("Dick");
s.students.emplace_front("Harry");
std::cout << s;
}
And all in one easy to cut-n-paste block:
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
class Student
{
public:
std::string name; // this is me being lazy. name should be private
Student(std::string inname):name(inname)
{
}
const std::string & getname() const
{
return name;
}
friend bool operator<(const Student & a, const Student &b)
{
return a.name < b.name;
}
};
class Section
{
public:
std::list<Student> students;
};
std::ostream& operator<<(std::ostream& out, const Section& section)
{
std::vector<std::reference_wrapper<const Student>> students;
// store references in the `vector`.
for (const auto & student: section.students)
{
students.push_back(std::ref(student));
}
// Sort the `vector`
std::sort(students.begin(), students.end());
// print the `vector`
for (const auto & student: students)
{
out << student.get().getname() << " ";
}
return out;
}
int main()
{
Section s;
s.students.emplace_front("Tom");
s.students.emplace_front("Dick");
s.students.emplace_front("Harry");
std::cout << s;
}
Or do what Remy suggested and use a std::vector<Student *> and a custom comparator to dereference the pointers for std::sort.
As others have stated, the error is because you are not dereferencing the iterator when populating your students[] array:
students[i] = pos; // <-- should be *pos instead!
I would suggest an alternative approach that should be faster and more efficient:
std::ostream& operator<< (std::ostream& out, const Section& section)
{
// 1. print the section header
out << setw(8) << left << section.getCourse()
<< setw(6) << left << section.getCallNumber();
out << ": " << section.getNumberOfStudents() << " students\n";
// 2. collect the students, sort, and print
std::vector<const Student*> students;
students.reserve(section.getNumberOfStudents());
for ( auto pos = section.students.cbegin();
pos != section.students.cend(); ++pos)
{
students.push_back(&(*pos));
}
sort (students.begin(), students.end(),
[](const Student *a, const Student *b) { return (*a < *b); }
);
for ( auto pos = students.cbegin();
pos != students.cend(); ++pos)
{
out << " " << *(*pos) << "\n";
}
out << flush;
return out;
}
I appreciate all your answers. Ended up being a much more basic issue. I had to implement the Section iterators to return student iterators.
Section::iterator Section::begin() {
return students.begin();
}
Section::const_iterator Section::begin() const {
return students.begin();
}
Section::iterator Section::end() {
return students.begin();
}
Section::const_iterator Section::end() const {
return students.begin();
}
Related
Here is part of my class assign_obj, constructor and operator that I want to print the object.
When trying to compile operator I am getting error :
error: 'A' was not declared in this scope for(assign_obj::item anItem : obj.*A){
Why is that?
If I try obj.A instead, I get error for the forloop as C++ can not loop a pointer of dynamic array.
class assign_obj{
private:
struct item{
char value;
int count;
};
item * A; //pointer A pointing to something of type Item
int size;
public:
assign_obj();
assign_obj(std::string);
friend std::ostream& operator<<(std::ostream & out, assign_obj & obj);
//Constructor
assign_obj::assign_obj(string aString){
size = aString.size();
A = new item[size];
item myItem;
for(int i = 0; i < size; i++){
myItem = {(char)toupper(aString[i]), 1};
A[i] = myItem;
}
}
// Print operator
std::ostream& operator<<(std::ostream & out, assign_obj & obj){
out <<"[ ";
for(assign_obj::item anItem : obj.*A){
out << anItem.value;
out << ":";
out << anItem.count;
out << " ";
}
out <<"]";
return out;
}
You can't use a for loop that way for a dynamically allocated array. You can use a plain old for loop for your plain old array.
std::ostream& operator<<(std::ostream & out, assign_obj & obj){
out <<"[ ";
for (size_t i = 0; i < obj.size; i++) {
out << obj.A[i].value << ":"
<< obj.A[i].count << " ";
}
out <<"]";
return out;
}
In c++ it is usually recomended to use std::vector, instead of raw c arrays.
You need to add:
#include <vector>
Then your member will be:
std::vector<item> A;
Allocating the items in assign_obj::assign_obj is done like this:
A.resize(size);
Finally your operator<< will be:
std::ostream& operator<<(std::ostream & out, assign_obj & obj){
out <<"[ ";
//-----------------------vvvvvvv----------vvvvv--
for(assign_obj::item const & anItem : obj.A){
out << anItem.value;
out << ":";
out << anItem.count;
out << " ";
}
out <<"]";
return out;
}
Notes:
Accessing the A member of obj is done with obj.A, not obj.*A.
Travresing the vector is done with a const& to avoid copy.
This question already has answers here:
Printing out contents of a list from the c++ list library [duplicate]
(5 answers)
Closed 7 months ago.
I am trying to add a list to a string.
int main() {
std::cout << "Hello, welcome to Jay's Coffee!!\n";
std::string name; std::cout <<"What is your name "; std::cin >> name;
std::cout <<"Hello " << name << ", thank you so much for coming in today";
std::list <std::string> menu = {"Black Coffee" "Espresso" "Latte" "Cappucino"};
std::cout << name <<",what would you like from our menu today? Here is what we are serving.\n" << menu;
}
Returns
invalid operands to binary expression ('basic_ostream<char>' and 'std::list<std::string>' (aka 'list<basic_string<char>>'))
There is no operator<< for lists. You have to write a loop. For example
for (auto& item : menu)
{
std::cout << item << '\n';
}
If you think about it it's obvious why you have to do this yourself. How are you doing to separate the list items? I've chosen to put each item on a new line. You might choose to separate them with commas or spaces or some fancy format. Because there is no obvious single way to print a list there is no predefined way in the C++ library.
You should write code this way. In c++, you can't print a list directly.
#include <string>
#include <list>
using namespace std;
int main() {
cout << "Hello, welcome to Jay's Coffee!!\n";
string name;
cout <<"What is your name ";
cin >> name;
cout <<"Hello " << name << ", thank you so much for coming in today";
list <string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
cout << name <<",what would you like from our menu today? Here is what we are serving.\n" ;
for ( string& s : menu )
{
cout << s << '\n';
}
}
The error message means that the operator << that you are trying to use with your object menu of the type std::list<std::string> is not defined for the class std::list<std::string>.
Also you need to separate strings in the initializer list with commas.
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
Otherwise the list will contain only one string due to the concatenation of string literals.
You could define such an operator as shown in the demonstration program below.
#include <iostream>
#include <string>
#include <list>
std::ostream & operator <<( std::ostream &os, const std::list<std::string>& lst )
{
for ( const auto &s : lst )
{
os << s << '\n';
}
return os;
}
int main()
{
std::list <std::string> menu =
{
"Black Coffee", "Espresso", "Latte", "Cappucino"
};
std::cout << menu;
}
The program output is
Black Coffee
Espresso
Latte
Cappucino
Or just use the range-based for loop directly in main like
std::cout << name <<",what would you like from our menu today? Here is what we are serving.\n";
for ( const auto &s : menu )
{
std::cout << s << '\n';
}
Or place the range-based for loop in a separate function similar to the operator << shown above.
First thing, you didn't define a list: in the declaration of menu, initializer-list elems were not separated by comma (,).
To make it reusable, I'd do it something like this:
#include <iostream>
#include <list>
static const std::string list_sep = ", ";
template<typename C> struct FormattedContainer
{
FormattedContainer(const C& cont, const std::string& sep = list_sep)
: cont_(cont)
, sep_(sep) {}
friend std::ostream& operator<<(std::ostream& os, const FormattedContainer& fc)
{
bool first = true;
for (auto&& e : fc.cont_)
{
if (first)
{
os << e;
first = false;
}
else
{
os << fc.sep_ << e;
}
}
return os;
}
const C& cont_;
const std::string sep_;
};
template<typename C>
auto make_fc(const C& cont, const std::string& sep = list_sep)
-> FormattedContainer<C>
{
return FormattedContainer<C>(cont, sep);
}
int main() {
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
std::cout << "What would you like from our menu today? Here is what we are serving.\n" << make_fc(menu);
}
This way, you don't need to define operator<< for something in std namespace (which might result in ambiguous calls as others might also define it, don't need to import another namespace, just simply call a wrapper around the type. You can use it with basically any container or iterable type using this method.
All other answers are correct but here is the better way to do the same
#include <iostream>
#include <algorithm>
#include <list>
template <typename T>
std::ostream & operator << (std::ostream & os, const std::list<T> & vec){
std::for_each (vec.begin () , vec.end() , [&](const auto& val){
std::cout << val << " ";
});
return os;
}
int main () {
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
std::cout << menu << "\n";
return 0;
}
I am trying to swap strings. For one of my functions of my class, I am passing in two strings and I also created a temp variable. I have been trying to compile my code, but it says "no suitable function for conversion from std::string to const char* exists.
void CdLib::swap(string *s1, string *s2)
{
string temp;
strcpy(temp, *s1);
strcpy(*s1, *s2);
strcpy(*s1, temp);
}
class CdLib
{
public:
int n;
char Cd[N_MAX];
string artist;
string title;
int year;
string genre;
string fan;
string imageURL;
CdLib();
void setFromFile(string fileName);
void print(string label);
void sortByYear();
void sortByArtist();
void sortByTitle(string genres[]);
private:
void swap(int *a, int *b);
void swapStrings(string *s1, string *s2);
};
I'm confused why it is trying to convert between string and char when they should all be string. Thank you.
strcpy() takes char* pointers, not string* pointers. It you are not allocating any memory for strcpy() to copy into.
Rather than using strcpy() at all, a better solution is to use std::string::operator= instead:
void CdLib::swap(string *s1, string *s2)
{
string temp = *s1;
*s1 = *s2;
*s1 = temp;
}
Or better, std::swap():
void CdLib::swap(string *s1, string *s2)
{
std::swap(*s1, *s2);
}
I am trying to swap strings
Why would you need to? Sorting can be accomplished via std::sort and you don't have to worry about how the strings get swapped - that's the beauty of C++, such basic operations are all implemented in the standard library.
std::swap supports pretty much everything, so use that.
Don't pass strings as arguments by value. Pass them by const reference. Return them by value. If a function is intended to modify a string in place, then it should take it by non-const reference (i.e. "just" a reference).
Don't write using namespace std - it's bad practice.
I guess that the CdLib class is some sort of a CD library, but you haven't told us what else your program should do.
If I were to write a sketch of this, I'd start with a structure representing the CD information, comparison functions for the CD that can be used in sorting, a function to print out the CD information, and a way to stream the CD information to/from an ostream/istream:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct CDInfo
{
std::string artist;
std::string title;
std::string genre;
std::string fan;
std::string imageUrl;
int year;
friend void swap(CDInfo& a, CDInfo& b)
{
// see https://stackoverflow.com/a/2684544/1329652 for rationale
using std::swap; // bring in swap for built-in types
swap(a.artist, b.artist);
swap(a.title, b.title);
swap(a.genre, b.genre);
swap(a.fan, b.fan);
swap(a.imageUrl, b.imageUrl);
swap(a.year, b.year);
}
};
bool lessByYear(const CDInfo &l, const CDInfo &r) {
return l.year < r.year;
}
bool lessByArtist(const CDInfo &l, const CDInfo &r) {
return l.artist < r.artist;
}
void print(std::ostream &os, const CDInfo &cd) {
os << "Artist: " << cd.artist
<< "\n Title: " << cd.title
<< "\n Genre: " << cd.genre
<< "\n Fan: " << cd.fan
<< "\n Image: " << cd.imageUrl
<< "\n Year: " << cd.year << "\n";
}
std::istream &operator>>(std::istream &is, CDInfo &cd)
{
std::string year;
std::getline(is, cd.artist);
std::getline(is, cd.title);
std::getline(is, cd.genre);
std::getline(is, cd.fan);
std::getline(is, cd.imageUrl);
if (std::getline(is, year)) cd.year = std::stoi(year);
return is;
}
std::ostream &operator<<(std::ostream &os, const CDInfo &cd)
{
os << cd.artist << '\n' << cd.title << '\n'
<< cd.genre << '\n' << cd.fan << '\n'
<< cd.imageUrl << '\n' << cd.year << '\n';
return os;
}
Then I'd write a class representing the CD library, with methods to access the individual CDs, iterators to access the entire collection, methods using the std::sort algorithm and the comparison functions to sort the library, and methods to load/save it from/to file, and to print the entire library (by default to stdout):
class CDLibrary
{
std::vector<CDInfo> m_CDs;
public:
CDLibrary() = default;
int count() const { return m_CDs.size(); }
void resize(int newCount) { m_CDs.resize(newCount); }
CDInfo &getCD(int index) { return m_CDs[index]; }
const CDInfo &getCD(int index) const { return m_CDs[index]; }
auto begin() { return m_CDs.begin(); }
auto end() { return m_CDs.end(); }
auto begin() const { return m_CDs.begin(); }
auto end() const { return m_CDs.end(); }
auto cbegin() const { return m_CDs.begin(); }
auto cend() const { return m_CDs.end(); }
void sortByYear() {
std::sort(begin(), end(), lessByYear);
}
void sortByArtist() {
std::sort(begin(), end(), lessByArtist);
}
void addCD(const CDInfo &cd) {
m_CDs.push_back(cd);
}
void removeCD(int index) {
m_CDs.erase(m_CDs.begin() + index);
}
bool load(const std::string &filename);
bool save(const std::string &filename) const;
void printAll(std::ostream &os = std::cout) const {
int n = 1;
for (auto &cd : *this) {
os << "--- CD #" << n << '\n';
print(os, cd);
}
}
};
Of course I'd also implement the streaming operators for the entire library, just as we did for the individual CDInfo:
std::istream &operator>>(std::istream &is, CDLibrary &lib) {
std::string count;
if (std::getline(is, count)) {
lib.resize(std::stoi(count));
for (auto &cd : lib)
if (!(is >> cd)) break;
}
return is;
}
std::ostream &operator<<(std::ostream &os, const CDLibrary &lib) {
if (!(os << lib.count() << '\n')) return os;
for (auto &cd : lib)
if (!(os << cd)) break;
return os;
}
Then, the load and save convenience methods can be expressed n terms of those streaming operators:
bool CDLibrary::load(const std::string &filename) {
std::ifstream ifs(filename);
try {
return ifs.good() && ifs >> *this;
} catch (...) {}
return false;
}
bool CDLibrary::save(const std::string &filename) const {
std::ofstream ofs(filename);
return ofs.good() && ofs << *this;
}
Hopefully this gives you some idea how such code might look. I'm not quite sure what you expected to achieve with void sortByTitle(string genres[]), so I didn't implement it. Feel free to comment under this answer to explain, as well as edit the question to make it clear what is the functionality you need.
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "World";
swap(str1,str2);
cout<<str1<<" ";
cout<<str2;
}
o/p:
Success #stdin #stdout 0s 4492KB
World Hello
I'm trying to overload the << operator to print a vector that contains elements of type Position (vector).
I managed to overload the << operator for type Position, but I can't figure out how to do it for vector. Can you help?
//position.h
#ifndef POSITION_H
#define POSITION_H
#include <iostream> // using IO functions
using namespace std;
class Position {
private:
int row;
int column;
public:
friend ostream& operator<<(ostream& os, const Position& P);
friend ostream& operator<<(ostream& os, const vector<Position>& VP);
};
#endif
//position.cpp
#include"Position.h"
#include <iostream> // using IO functions
#include<vector>
ostream& operator<<(ostream& os, const Position& P)
{
os << '(' << P.row << ',' << P.column << ')' << endl;
return os;
}
ostream& operator<<(ostream& os, const vector<Position>& VP)
{
Position placeholder;
for (int i = 0; i != VP.size(); i++)
{
placeholder = VP.at(i);
cout << placeholder << endl;
}
return os;
}
int main()
{
Position C1(2, 1);
Position C2(3, 1);
Position C3(4, 1);
vector<Position> cans;
cans.push_back(C1);
cans.push_back(C2);
cans.push_back(C3);
cout << cans;
system("pause");
}
I'm sure there's a duplicate on StackOverflow somewhere, but I cannot find it. I'm a bit surprised about this question, as I don't really see a critical issue at first sight. Did you get a compiler error or segmentation fault?
Every time you assign to Placeholder, you make a copy. This is not necessary at all: you can directly access the element at the index. As you limit the index to 0 to size(), you don't have to worry about going out-of-bounds, so don't use .at(i), which throws an exception if you go out of bounds. Just use VP[i].
Also not that std::endl flushes the buffer, which is slow. A normal enter/end-line can be achieved with the '\n' character.
Finally, using namespace std; is considered bad practice.
You have multiple options for processing the elements of the vector:
index-based for loop:
for (int i = 0; i != VP.size(); ++i) {
os << VP[i] << '\n';
}
Iterator-based for loop
for (auto it = cbegin(VP); it != cend(VP); ++it) {
os << *it << '\n';
}
range-based for loop
for (auto const& el : VP) {
os << el << '\n';
}
Algorithm for(each) loop
std::for_each(cbegin(VP), cend(VP),
[](auto const& el) { os << el << '\n'; });
Copy to ostream_iterator
std::copy(cbegin(VP), cend(VP),
std::ostream_iterator<Position>(os, "\n"));
Note that here you could also write <decltype(VP)::value> instead of <Position> to keep it generic, but that might be overkill in this situation.
And the last two have a C++20 ranges equivalent:
std::ranges::for_each(VP,
[](auto const& el) { os << el << '\n'; });
std::ranges::copy(VP,
std::ostream_iterator<Position>(os, "\n"));
Note:
friend ostream& operator<<(ostream& os, const vector<Position>& VP); doesn't have to be a friend of Position! It doesn't require access to the private members.
I am trying to overload the =operator on line 9 in the code below:
void searchContact(vector<Person> &people){
string searchTerm;
vector<Person>::iterator it;
cout << endl;
cout << "Enter search term: ";
getline(cin, searchTerm);
it = find(people.begin(), people.end(), searchTerm);
if (it != people.end()){
cout << "Element found in: " << *it << '\n';
}else{
cout << "Element not found\n";
}
}
My approach is this:
int data;
Person& operator=(Person& a) { return a; }
Person& operator=(int a) {
data = a;
return *this;
}
I am getting this error:
class.cpp:129:30: error: ‘Person& operator=(Person&)’ must be a nonstatic member function
Person& operator=(Person& a) { return a; }
^
class.cpp:130:26: error: ‘Person& operator=(int)’ must be a nonstatic member function
Person& operator=(int a) {
What is wrong with my approach, or am i doing it all wrong from the beginning?
First, you are overloading the wrong operator. std::find() uses operator== (comparison) instead of operator= (assignment). And, given that you are passing a std::string to std::find(), you need an operator== that takes a std::string as input, not a Person.
Second, you are trying to implement the operators as unary operators, which means they MUST be non-static members of your Person class. The compiler is complaining that they are not.
Third, if std::find() finds a match, you are passing *it to std::cout, so you need an overloaded operator<< that takes a Person for output.
Try something like this:
class Person
{
public:
...
bool operator==(const string &rhs) const
{
// compare members of *this to rhs as needed...
return ...; // true or false
}
/* alternatively:
friend bool operator==(const Person &lhs, const string &rhs)
{
// compare members of lhs to rhs as needed...
return ...; // true or false
}
*/
friend ostream& operator<<(ostream &out, const Person &p)
{
// output p to out as needed...
return out;
}
...
};
Then your search code will work:
void searchContact(vector<Person> &people)
{
cout << endl;
cout << "Enter search term: ";
string searchTerm;
getline(cin, searchTerm);
vector<Person>::iterator it = find(people.begin(), people.end(), searchTerm);
if (it != people.end()) {
cout << "Element found in: " << *it << '\n';
} else {
cout << "Element not found\n";
}
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Person{
private:
int data;
public:
Person(int data)
{
this->data = data;
}
int getData()
{
return data;
}
friend bool operator==( const Person &lhs, const int rhs);
};
bool operator== ( const Person &lhs, const int rhs )
{
return lhs.data == rhs;
}
void searchContact(std::vector<Person> &people){
int searchTerm = 1;
vector<Person>::iterator it;
it = find(people.begin(), people.end(), searchTerm);
if (it != people.end()){
cout << "Element found in: " << it->getData() << '\n';
}else{
cout << "Element not found\n";
}
}
int main(int argc, char **argv)
{
std::vector<Person> list1 = {1, 2, 3, 4};
std::vector<Person> list2 = {1, 2, 3, 5};
std::vector<Person> list3 = {1, 3, 7, 6, 9, 5, 2, 4};
searchContact(list1);
searchContact(list2);
searchContact(list3);
}
i don't know what do you want. please show your full code and tell what do you want.
if you want to find element in vector, you can write like this(not string. if ou want to find string, just change int to string and get data with cin or etc)