Why I get address when deferencing begin()? - list

#include <list>
#include <string>
#include <iostream>
int main()
{
std::list<std::string*> *listStr = new std::list<std::string*>();
listStr->push_back(new std::string("HI"));
std::cout << *(listStr->begin()) << std::endl;
return 0;
}
I think I should be getting HI, but I got address as output
008A2B10 Press any key to continue . . .
I can't find my mistake..or have I misunderstood something?

You're printing the pointer in the container.
You need one dereference for the iterator, and another one for the pointer to the string object.

It prints string *, not string.
To print string:
#include <list>
#include <string>
#include <iostream>
int main()
{
std::list<std::string> *listStr = new std::list<std::string>();
listStr->push_back(std::string("HI"));
std::cout << *(listStr->begin()) << std::endl;
return 0;
}

Related

C++ bitset strange value [duplicate]

This question already has answers here:
Using scanf/printf to input into/output from a bitset
(3 answers)
Closed 5 months ago.
#include <bitset>
#include <assert.h>
#include <stdio.h>
using namespace std;
int main()
{
bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
printf("bs[11]=%d\n", bs[11]);
printf("bs[12]=%d\n", bs[12]);
return 0;
}
console output:
Why can't I simply get 0 or 1 as output ?
printf with %d is for integer values, whereas std::bitset::operator[] returns a std::bitset::reference.
You can use std::cout from <iostream> header (which is anyway a more c++ "way" to print to the console):
#include <bitset>
#include <assert.h>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11] = 0;
bs[12] = 1;
assert(bs[12] == 1);
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
Output:
bs[11]=0
bs[12]=1
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
With some review comments :
#include <cassert>
#include <bitset>
#include <iostream>
// anything with a .h extension is probably "C" not "C++"
// #include <assert.h>
//#include <stdio.h>
// using namespace std; <== NO, don't use using namespace std;
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
std::cout <<"bs[11]" << bs[11] << "\n";
std::cout << "bs[12]" << bs[11] << "\n";
return 0;
}
If you are using C++ then don't call printf to output something (my compiler refuse to compile your code correctly).
This C++ code works correctly using iostream:
#include <bitset>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}

How could/should I print text strings above number columns in C++?

I would like to print the strings at the top of columns with a 1 x 3 array.
I have edited this simple function several times, and this produces the least errors. New to C++, reading Deital Chap 6 Recursive.
What am I missing? I started with half brackes around strings, and brackets seemed to produce less errors.
Here is the code:
#include <iostream>
#include <string>
#include <array>
using namespace std;
int main() {
array a[1][3] = ["Car" "Hours" "Charge"]
cout<< a << endl;
}
Terminal produces errors as such:
parking_charges_6_12.cpp: In function ‘int main()’:
parking_charges_6_12.cpp:8:7: error: missing template arguments before ‘a’
8 | array a[1][3] = ["Car" "Hours" "Charge"]
^
This should work:
#include <array>
#include <iostream>
#include <string>
int main(){
std::array<std::string, 3> headlines = {"Car", "Hours", "Charge"};
for( auto const& elem : headlines ){
std::cout << elem << "\t";
}
}
It should be curly braces {} in the initializer, not []. And you need a comma between each element.
On the other hand, in later C++ revisions array can detect the type and number of elements, so you don't have to give that.
#include <iostream>
#include <array>
using namespace std;
int main() {
array a = {"Car", "Hours", "Charge"};
for (auto& item : a)
cout<< item << endl;
}
How about something like this:
#include<iostream>
using namespace std;
int main() {
string data[3] = {"Car", "Hours", "Charge"};
for (int i = 0; i < 3; i++)
cout << data[i] << " ";
}
Obviously it is not using the array header, but it's a working example. If you do need to use the array header, you can try something like :
#include <array>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main() {
array<string, 3> ar3 = {"Car", "Hours", "Charge"};
cout << ar3.size() << endl;
for (auto i : ar3)
cout << i << ' ';
return 0;
}
You can see it working online here

How can I convert my small program into a function for use in a project?

I have the following small program to find the date a file was last modified. I need to turn it into a function so I could call the program in another function in a big project. I'm not sure how to turn my program into a function.
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iomanip>
struct stat buf;
void convertEpochTime()
{
std::uint32_t time_date_stamp = buf.st_mtime;
std::time_t temp = time_date_stamp;
std::tm* t = std::gmtime(&temp);
std::cout << "[" << std::put_time(t, "%m-%d-%Y %I:%M:%S %p") << "]";
}
int main()
{
stat("testoutput.txt",&buf);
convertEpochTime();
return 0;
}
What if you do something like:
#include <a>
#include <b>
#include <c>
...
void ABC() {
stat("testoutput.txt",&buf);
// Migrated all the code of the function into a single function.
// WARNING: If the function returns something, you need to assign
// the value into the variable.
std::uint32_t time_date_stamp = buf.st_mtime;
std::time_t temp = time_date_stamp;
std::tm* t = std::gmtime(&temp);
std::cout << "[" << std::put_time(t, "%m-%d-%Y %I:%M:%S %p") << "]";
}
.
.
.
// after everything >>>
int main(void) {
A_very_big_program();
more_functions();
...
ABC();
return 0;
}
Just rename the function name into something that must not be main() or any predefined ones, migrate the entire code (efficiently a void function which doesn't returns anything neither needs to assign anything into it, mentioned in the warning).

How can I store a text file in a stack using fstream

#include <iostream>
#include <stack>
#include <fstream>
using namespace std;
int main()
{
stack <fstream> webpages;
fstream web1;
fstream web2;
fstream web3;
web1.open("web1.txt",fstream::out);
web2.open("web2.txt",fstream::out);
web3.open("web3.txt",fstream::out);
web1.close();
webpages.push(web1);
cout << webpages.size() << endl;
system("pause");
}
When I compile the program I get the following error:
std::basic_fstream>::basic_fstream(const
std::basic_fstream> &): attempting to
reference a deleted function
so I assume the way I am doing this is wrong. Is there a way I can store fstream files in a stack?
I'm assuming you want to store the text in the files on your stack and not the actual fstream objects? If so, it will be easier to store the content of the files as strings.
#include <iostream>
#include <stack>
#include <fstream>
#include <sstream>
int main()
{
std::stack <std::string> webpages;
for(const auto& file : {"web1.txt", "web2.txt", "web3.txt"}) {
std::fstream page(file);
std::stringstream ss;
ss << page.rdbuf();
webpages.emplace(ss.str());
}
std::cout << webpages.size() << "\n"; // will print "3"
std::cout << "-- pages read --\n";
for(;!webpages.empty(); webpages.pop()) {
std::cout << webpages.top() << "\n";
}
}
Edit: As pointed out, it really looks like you want to open some files for writing, so here's a version for that.
#include <stack>
#include <fstream>
int main()
{
std::stack<std::fstream> webpages;
for(const auto& file : {"web1.txt", "web2.txt", "web3.txt"}) {
webpages.emplace(file, std::fstream::out);
}
for(;!webpages.empty(); webpages.pop()) {
webpages.top() << "some text\n";
}
return 0;
}

Vectors and Strings C++

#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
}
Why is this code giving up an error? I am unable to identify the cause of the error.
Error: No Match for operator !=....
First of all, you are trying to compare int with the iterator of vector.
for(int i=0; i!=test.end();i++)
{
cout << test[i];
}
Here, the test.end() returns the iterator. There is no overloaded operator!= which can compare integer (int i = 0) with that iterator (test.end()).
So your loop should look more like:
for (std::vector<string>::iterator i = test.begin(); i != test.end(); i++)
{
cout << *i;
}
You can replace std::vector<string>::iterator with auto, if using C++11 or newer.
The next thing, you included <string.h> which contains old functions such as: strlen, strcpy. Similarly, <cstring> contains C-style strings.
If you want to you use operator<<, so if you want to write:cout << then you have to do: #include <string>.
As already mentioned, the problem is, that you try to compare an integer with an iterator in the "middle" of your for statement. Try this instead, it's more intuitive from my point of view
#include <vector>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
int main()
{
vector<string> test;
test.push_back("yasir");
test.push_back("javed");
for(int i=0; i<test.size();++i)
{
cout << test[i];
}
}