• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Slavik81

Member
v.resize(v.size()); doesn't do anything. For example, here's the libstdc++ implementation:
Code:
void resize(size_type __new_size)
      {
        if (__new_size > size())
          _M_default_append(__new_size - size());
        else if (__new_size < size())
          _M_erase_at_end(this->_M_impl._M_start + __new_size);
      }

v.shrink_to_fit() is also probably not what's requested. It doesn't get rid of any elements of the vector. Pinewood mentioned wanting something like erase, and it's not really similar.

To understand what shrink_to_fit does, first you need to understand how vector allocates memory. When new elements are added to a vector (like via push_back), it needs more memory. Since vector's storage is guaranteed to be continguous, acquiring more memory typically means requesting a fresh new, larger chunk of memory, moving all existing items into that new memory and deleting the old memory. As you can imagine, that's a rather expensive process. If it happened after every call to push_back, it would be a problem, so vector will generally allocate 2x as much memory as required. That way, it only needs to reallocate every once in a while. All the rest of the time, it can just expand into that extra capacity it had set aside.

shrink_to_fit requests another reallocation that is sized as close as possible to the size of the vector. It's rarely useful. You'd only really want to use it you have a vector that was created through a series of push_backs, that will be used unmodified throughout the lifetime of your application.
So how would I go and remove empty elements from a vector in C++?
To clarify, I know that I can use erase, but how can I conviniently remove all empty cells at the same time?
What do you mean by 'empty'? If you can implement a function with a signature like:
Code:
bool is_empty(const T& value);
where T is the type of the element, then you can use std::remove_if to move the elements to the end of the vector, and erase to get rid fo them.

Code:
v.erase(std::remove_if(v.begin(), v.end(), is_empty), v.end());
 

Sharp

Member
Very general question, are there any particularly good resources out there for learning SQL? I'm aware of W3Schools and had a look at LSQLTHW but if seems incomplete. Any others out there?
I actually find the official PostgreSQL documentation to be a pretty good guide. Check out the tutorial first and then you'll have access to everything else you could possibly want to know about the database, including internal implementation details, on the same site.

If you have a more theoretical mindset, http://www.amazon.com/dp/1449316409/?tag=neogaf0e-20 is a pretty great book for understanding the mathematics behind relational database management systems.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
v.resize(v.size()); doesn't do anything. For example, here's the libstdc++ implementation:

v.shrink_to_fit() is also probably not what's requested. It doesn't get rid of any elements of the vector. Pinewood mentioned wanting something like erase, and it's not really similar.

Ah, I see.
 

Pinewood

Member
What do you mean by 'empty'? If you can implement a function with a signature like:
Code:
bool is_empty(const T& value);
where T is the type of the element, then you can use std::remove_if to move the elements to the end of the vector, and erase to get rid fo them.

Code:
v.erase(std::remove_if(v.begin(), v.end(), is_empty), v.end());

Well, if I want to remove and element from a vector, I make the "slot" equal with NULL. How would I check that with the is_empty command?
 

Slavik81

Member
Well, if I want to remove and element from a vector, I make the "slot" equal with NULL. How would I check that with the is_empty command?
Code:
bool is_empty(T* value) { return value == NULL; }
Though, in this case you could use std::remove, which compares against a given value, rather than std::remove_if, which calls a function.
Code:
v.erase(std::remove(v.begin(), v.end(), NULL), v.end());
 

usea

Member
pY6QPrZ.png
I made this because when I run my integration tests at work I basically can't do anything for 6-8 minutes. Waiting on code to compile is so passé.

(original)

edit: I didn't make most of the tests. This project uses entity framework everywhere. To test almost anything you have to supply at least a dbset mock, but more likely spin up a database. So we have about 200 tests that each spin up a new database, register a ton of things with an IoC container, etc.
 
I made this because when I run my integration tests at work I basically can't do anything for 6-8 minutes. Waiting on code to compile is so passé.

(original)

edit: I didn't make most of the tests. This project uses entity framework everywhere. To test almost anything you have to supply at least a dbset mock, but more likely spin up a database. So we have about 200 tests that each spin up a new database, register a ton of things with an IoC container, etc.

Yeah, I'm in the same boat, I write web application tests (tests that run in a browser) in Ruby at work and those can also take a couple of minutes to execute, depending on the complexity.
 

robin2

Member
Installing python (3.4.2) on a vagrant vm with lubuntu64.

I've never used linux in any form so I was sort expecting some difficulties, but I didn't expect an ordeal of googling and failures.
(Should I use a different linux than lubuntu?)

After some failed attempts, I found out that there are "requisites" installations to do before installing (or build, since it's not an installer) python; stuff like:

$ sudo apt-get install build-essential
$ sudo apt-get install libreadline-gplv2-dev
$ sudo apt-get install libsqlite3-dev
$ sudo apt-get install libbz2-dev

Is there a complete and up-to-date list of these (I googled some lists but they always include libs that are not found)? On the python official documentation they don't seem even mentioned (only one is "libsqlite3", buried away in a wall of text).
Is the following one ok? Something missing/wrong?
build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev git
 
How are you installing python? If you just install it with apt-get, it'll resolve the dependencies automatically and you won't have to do anything. Just do apt-get install python3 (the package name might not be exactly that) and you're done.
 

robin2

Member
How are you installing python? If you just install it with apt-get, it'll resolve the dependencies automatically and you won't have to do anything. Just do apt-get install python3 (the package name might not be exactly that) and you're done.
I downloaded the ..tarball it think it's called, from the official site.
then compiled (i think) it, after extraction with:
./configure
make
make altinstall

At the beginning I tried with "apt-get python3" but it seemed, to me, that it installed the 3.4.0, which is already pre-installed on ubuntu, so I wasn't sure if I was installing something or not (and I read that the built-in version of python should be left alone).
I also think that modules, such as virtualenv, didn't work correctly or at all; but I've tried so many times after that, I may be misremembering. I remember that virtualenvwrapper never worked ever.
 
I downloaded the ..tarball it think it's called, from the official site.
then compiled (i think) it, after extraction with:
./configure
make
make altinstall

At the beginning I tried with "apt-get python3" but it seemed, to me, that it installed the 3.4.0, which is already pre-installed on ubuntu, so I wasn't sure if I was installing something or not (and I read that the built-in version of python should be left alone).
I also think that modules, such as virtualenv, didn't work correctly or at all; but I've tried so many times after that, I may be misremembering. I remember that virtualenvwrapper never worked ever.

You shouldn't really have to fiddle with tarballs on *buntus unless you want to build from the source for a specific reason.

What does calling "python" on the console say?

If it opens the python console, write

>>> import sys
>>> print(sys.version)

What does that say?
 

robin2

Member
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
>>> print(sys.version)
2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2]

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
>>> print(sys.version)
3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2]


This with the freshly built vm.

From what I've read:
Those are the built-in python installations, which I should "leave alone" because they're used by the system itself; installing modules (with pip) in them is not good, because if something is screwed the whole system won't work; so I should make new python installations, alongside the exiting ones.
Those are in "usr/bin", while the new ones I have to do, place themselves in "usr/local/bin" (currently empty); in these new ones I can mess as much as I want.

So I wanted to make that new coexisting installation of Python 3.4.2. I managed to do this with using the process "create/make/makealtinstall" described in my above post, but then I've had problems with modules: missing libs and modules still referring to the built-in installations, despite me doing stuff like:
export PATH="/usr/local/bin":$PATH
(and many other variants) which should point to the new interpreter in "usr/local/bin", instead of the built-in "usr/bin".

I don't know.
 

Onemic

Member
Can anyone tell me whats going on here with this JS file? Not sure why this isn't running:

Code:
function show() {
    if (httpRequest.readyState === 4) {
        alert("response received");
        if (httpRequest.status === 200) {
            alert("Server Response!");
            var jsObj = JSON.parse(httpRequest.responseText);
            var str = "<h1><mark>INT222 - Lab 06</mark></h1>";
            str += "<table>";
            str += "<caption>School of ICT | Faculty of Applied Science and Engineering Technology</caption>";
            str += " <thead> <tr><th>Students</th><th> Program</th><th>Semester</th><th colspan=5 id=course>Courses</th></tr></thead><tbody>";
            alert(str);
            for (var i = 0; i < jsObj.length; i++) {
                str += "<tr><td>" + jsObj[i].name + "</td>";
                str += "<td>" + jsObj[i].semester + "</td>";
                str += "<td>" + jsObj[i].program + "</td>";
                for (var j = 0; j < jsObj.courses.length; j++) {
                    str += "<td>" + jsObj[i].courses[j] + "</td>";
                }
                str += "</tr>";
            }
            str += "</tbody></table>";
            document.getElementById("data").innerHTML = str;
        } else {
            alert("problem with request");
        }


    }

}
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Can anyone tell me whats going on here with this JS file? Not sure why this isn't running:

Can you post output/errors you get from the console?
Change your alerts to console.log's if need be. I personally find that easier for debugging than having alerts popping up all the time.
 

Quasar

Member
So with Android Studio 1.0 anyone using this? care to comment on its quality versus other IDEs?

A related question, for someone a little familiar with Java (but has never done any mobile development), where to start with an Android development guide?
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
So with Android Studio 1.0 anyone using this? care to comment on its quality versus other IDEs?

A related question, for someone a little familiar with Java (but has never done any mobile development), where to start with an Android development guide?

I'm using it. It's quality for Android programming is very good in my opinion. Jetbrains makes some of the best IDE's out there, and Google built Android Studio off their IntelliJ platform.

To start with, go to Google themselves. They have an extensive development resource center with tutorials on writing some apps. There used to be a free Udacity course on building a weather app, but I'm not sure if it's available anymore. Start here
 

Husker86

Member
So with Android Studio 1.0 anyone using this? care to comment on its quality versus other IDEs?

A related question, for someone a little familiar with Java (but has never done any mobile development), where to start with an Android development guide?

I'm using it. It's quality for Android programming is very good in my opinion. Jetbrains makes some of the best IDE's out there, and Google built Android Studio off their IntelliJ platform.

To start with, go to Google themselves. They have an extensive development resource center with tutorials on writing some apps. There used to be a free Udacity course on building a weather app, but I'm not sure if it's available anymore. Start here

Definitely go with Android Studio. Especially now that it's 1.0. Eclipse/ADT will be supported for a while, I assume, but I can't think of a solid reason to stick with it, especially if you're just starting with Android.

Udacity's course is here, the "Course Materials" on Udacity are always free.

https://www.udacity.com/course/ud853
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
That's the one! Thanks for grabbing that link. I didn't realize that Udacity gave out the course materials for free.

Definitely go with that one. It's a great starter course that covers a lot of the tech that's used when building a modern app.
 

danthefan

Member
I actually find the official PostgreSQL documentation to be a pretty good guide. Check out the tutorial first and then you'll have access to everything else you could possibly want to know about the database, including internal implementation details, on the same site.

If you have a more theoretical mindset, http://www.amazon.com/dp/1449316409/?tag=neogaf0e-20 is a pretty great book for understanding the mathematics behind relational database management systems.

Much obliged.
 

phoenixyz

Member
From what I've read:
Those are the built-in python installations, which I should "leave alone" because they're used by the system itself; installing modules (with pip) in them is not good, because if something is screwed the whole system won't work; so I should make new python installations, alongside the exiting ones.
Those are in "usr/bin", while the new ones I have to do, place themselves in "usr/local/bin" (currently empty); in these new ones I can mess as much as I want.

So I wanted to make that new coexisting installation of Python 3.4.2. I managed to do this with using the process "create/make/makealtinstall" described in my above post, but then I've had problems with modules: missing libs and modules still referring to the built-in installations, despite me doing stuff like:
export PATH="/usr/local/bin":$PATH
(and many other variants) which should point to the new interpreter in "usr/local/bin", instead of the built-in "usr/bin".

You should use the version that comes with Ubuntu and just install the modules you need. It's a vagrant VM isn't it? The chances that something breaks are rather low. And if it does just use virtualenv instead.
Compiling your own Python is not really the way to do it imho. While it's much easier than doing it on Windows you can save yourself some headaches by just using the packages.
 
You'd be surprised at the common user, especially since they probably don't delete the Untitled that comes up as default on a Word Document.

Doesn't the rename box routinely select the old title so that starting to type there automatically removes the previous title? I use a number of different GUIs, but I think I'm correct to say this is default behaviour in all of them. You would actually have to press the End key, the Home key, or a cursor movement key to override the selection.
 

moka

Member
I don't get it. Who gives their files such stupid names?

Sometimes at work we patch production (I know)...

Sometimes something comes up and we have to patch the same file again...

Sometimes when I'm annoyed and not thinking, I call the old patch 'oldPatch'.

This works fine until we need to patch again. ;)
 
Sometimes at work we patch production (I know)...

Sometimes something comes up and we have to patch the same file again...

Sometimes when I'm annoyed and not thinking, I call the old patch 'oldPatch'.

This works fine until we need to patch again. ;)

The first thing I missed in moving from VAX/VMS to SunOS and eventually other Unixes was that the file system had no concept of file revisions. In VMS this was such a ubiquitous concept, and so useful, that I'm still surprised that it was never adopted outside the VMS community.

Of course, that doesn't solve the problem of badly annotated file copies and badly chosen filenames, which is what the xkcd cartoon is about. It's basically a primitive revision control system.
 

-duskdoll-

Member
Guys i need your help! I'm supposed to write a program
(i actually need to write 4 programs but this is the most confusing one)
in C that takes longitude and latitude from the user and displays the location on the screen, something like a radar i suppose. The thing is, i don't know how on earth i can "map" the screen (i'm guessing the command prompt screen). You don't have to give me the code, just the method or at least point me to the right direction. I also have to make use of a function called "go somewhere" if that matters.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Define the point of origin for your display, (usually top left corner), then translate your user's GPS position into UTM coordinates and overlay their position with an X/Y coordinate.

At least this is how we handle it. The implementation may be different for you, but it might give you a general idea.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I'm a freshman and that sounds wayyy too complicated. Isn't there a very basic way to do this?

Define a reference point and then translate the long/lat into X/Y and display the point based on that?

That sounds like a fairly complicated project to give to a freshman. I'm guessing there has been some material covered in the course discussing this. Otherwise head to your prof's office and ask for some guidance.
 

Quasar

Member
Definitely go with Android Studio. Especially now that it's 1.0. Eclipse/ADT will be supported for a while, I assume, but I can't think of a solid reason to stick with it, especially if you're just starting with Android.

Udacity's course is here, the "Course Materials" on Udacity are always free.

https://www.udacity.com/course/ud853

Thanks.

Funny, even with the supplied solution, let alone my version I get issues.

I've been having this issue with nullpointerexceptions.

I even see it when I replaced my code with the solution provided on github here:

https://github.com/udacity/Sunshine...le/android/sunshine/app/ForecastFragment.java

I get an error like this:

Code:
12-13 07:06:48.050    1658-1779/local.sunshine D/dalvikvm&#65109; GC_FOR_ALLOC freed 214K, 7% free 4488K/4812K, paused 3ms, total 11ms
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Fri, Dec 12 - Clouds - 18/18
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Sat, Dec 13 - Rain - 21/15
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Sun, Dec 14 - Rain - 27/13
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Mon, Dec 15 - Rain - 26/16
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Tue, Dec 16 - Clear - 29/16
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Wed, Dec 17 - Rain - 32/19
12-13 07:06:48.060    1658-1779/local.sunshine V/FetchWeatherTask&#65109; Forecast Entry: Thu, Dec 18 - Rain - 27/18
12-13 07:06:48.060    1658-1658/local.sunshine D/AndroidRuntime&#65109; Shutting down VM
12-13 07:06:48.060    1658-1658/local.sunshine W/dalvikvm&#65109; threadid=1: thread exiting with uncaught exception (group=0xb0c96b20)
12-13 07:06:48.060    1658-1658/local.sunshine E/AndroidRuntime&#65109; FATAL EXCEPTION: main
    Process: local.sunshine, PID: 1658
    java.lang.NullPointerException
            at local.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:287)
            at local.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:103)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Now in terms of the application, it seems to still work: the dummy data is displayed on start, and with a refresh the data grabbed from openweathermap displayed.

And looking at the two ForecastFragment references I'm not really sure why I'm getting nullpointerexceptions
 

Onemic

Member
Can you post output/errors you get from the console?
Change your alerts to console.log's if need be. I personally find that easier for debugging than having alerts popping up all the time.

I'm good now, thanks. Fixed it. Problem was my syntax. Tried accessing Courses where it didnt exist.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I'm good now, thanks. Fixed it. Problem was my syntax. Tried accessing Courses where it didnt exist.

Awesome that you fixed it, and thanks for the update with your solution.

--------

Have any of you put some time into Firefox Developer Edition?
And if so, what are your impressions?

I'm about ready to ditch Chrome altogether, and Firefox is looking to be a worthy candidate.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Anyone have any experience working with selenium with python? I've successfully created an application that opens a browser to the desired website whenever needed but the chrome browser is completely vanilla. I've tried looking into the documentation concerning loading addons on browser calls, but I have had just absolutely no luck getting anything going there.
 

Slavik81

Member
I made this because when I run my integration tests at work I basically can't do anything for 6-8 minutes. Waiting on code to compile is so passé.

(original)

edit: I didn't make most of the tests. This project uses entity framework everywhere. To test almost anything you have to supply at least a dbset mock, but more likely spin up a database. So we have about 200 tests that each spin up a new database, register a ton of things with an IoC container, etc.
I have actually never encountered slow tests that were deterministic. They typically had a 1 in 1000 chance of failing. This, of course, became a problem once we had 1000 tests.

After a lot of work to fake out slow, asynchronous systems, the only slow part of our tests was building them.
 

Husker86

Member
I have a question about public repos on Github.

Is there a way I can remove/obscure my Google tracking ID, or is that something I don't need to worry about? I suppose anyone could see it when viewing source of my webpage...maybe I just answered my own question.
 
I made this because when I run my integration tests at work I basically can't do anything for 6-8 minutes. Waiting on code to compile is so passé.

(original)

edit: I didn't make most of the tests. This project uses entity framework everywhere. To test almost anything you have to supply at least a dbset mock, but more likely spin up a database. So we have about 200 tests that each spin up a new database, register a ton of things with an IoC container, etc.

I hate tests that rely upon the database. Slow running tests often enough are never run.

I don't know if you've done the same, but where we could, we used a repository pattern so the business logic wouldn't depend on the EF context directly, and the repository(/ies) the logic relied upon only exposed the minimum needed for that particular piece of code. For example, if the logic only needed to work with the "Bars" table as data, we passed it an IBarRepository* or, often enough, an IRepository<Bar>**. (For logic that required access to multiple tables, we'd bundle together repositories into a unit of work interface.) This allowed us to write unit tests for the business logic and reserve the slower integration tests for the concrete repository classes that were tested independently of any business logic tests.

Of course, that project ended up going off the rails at some point. Large banks, small budgets, what are you going to do.

-----
**IRepository<T> got you CRUD methods "for free" when the concrete Repository<T> class was used. Similarly, substitution for unit tests was also "free" with a StubRepository<T> instance.
*I{T}Repository implementations were reserved for when the free methods above were not required or were insufficient (such as needing specialized queries), as the case may be. These repositories might inherit from the above or go their own different direction, depending upon need.
 
Top Bottom