Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


TheComet

Member Since 02 Oct 2013
Offline Last Active Today, 10:25 AM

Posts I've Made

In Topic: C++ Ternary operator ?: surprising conversion rules.

22 April 2015 - 06:58 PM


What does that code print? Try to guess without running it...

 

What does this do, and what is this called? I've never seen this before:

operator double() { return x; }

In Topic: Why didn't somebody tell me?

28 March 2015 - 08:28 AM

It's called the alphabet because the first two greek letters are "alpha" and "beta" --> "alpha" + "bet"


In Topic: what is the origin of your nickname/Internet handle?

23 March 2015 - 11:47 PM

I was 9 years old and it sounded cool.


In Topic: c++ count lines in txt file and then read these lines without reopening a file

23 March 2015 - 11:21 PM

@WiredCat - This might be interesting to you: http://insanecoding.blogspot.ch/2011/11/how-to-read-in-file-in-c.html

Speed test of various file reading methods in C++.

 

 

 


and to people that said use vector no i wont because i dont want to recreate the array almost every line.

Vectors don't reallocate every line... They (usually) reallocate log(n) because they expand exponentially. If you read 5000 lines, the vector will reallocate ~12 times. Look, here's an example:

int main()
{
    std::vector<int> v;
    for(int i = 0; i != 5000; ++i)
        v.push_back(i);
    return 0;
}

Output:

$ valgrind ./test                       
==5628== HEAP SUMMARY:
==5628==   total heap usage: 14 allocs, 14 frees, 65,532 bytes allocated

See? 14 allocations, even though I pushed 5000 elements into it. That's not "every line".

 

Here's my fancy solution (C++11):

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>


class Line
{
    std::string data;
public:
    friend std::istream& operator>>(std::istream& is, Line& l)
    {
        std::getline(is, l.data);
        return is;
    }
    std::string operator() const { return data; }
};  


std::vector<std::string> get_file_lines(std::string file_name)
{
    std::ifstream in(file_name.c_str(), std::ios::in);
    return std::vector<std::string> {std::istream_iterator<Line>{in},
                                         std::istream_iterator<Line>{}};
}


int main()
{
    auto lines1 = get_file_lines("file.txt");
    return 0;
}

http://stackoverflow.com/questions/1567082/how-do-i-iterate-over-cin-line-by-line-in-c/1567703#1567703

http://stackoverflow.com/questions/236129/split-a-string-in-c


In Topic: Is this concerning or just laughable?

23 March 2015 - 01:11 PM


Where I live, respect is something that you give to people by default until they have proven themselves unworthy of respect. It is disrespect that is earned. If you don't respect me when I've done nothing to earn your disrespect, then I would say that you are being an asshole.

Disrespect != Being an Asshole.

 

If I meet someone for the first time, they don't have my respect. I treat them neutrally. If you expect me to respect you when we don't know each other, that's when I start disrespecting you, because you would be the asshole in that situation.


PARTNERS