|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On January 28 2015 07:18 Z-BosoN wrote:Show nested quote +On January 28 2015 07:13 solidbebe wrote: btw is there a difference between ++i and i++? ++i updates i, then does whatever, while i++ does whatever, then updates i i = 9 ++i == 9 is FALSE, because i incremented before checking the equality i++ == 9 is TRUE, because the original value was used in the equality before getting incremented in both instances i is 10 after the increment
To put this into perspective:
while (iter != list.end() { list2.push_back(iter++); }
is equal to:
while (iter != list.end() { list2.push_back(iter); ++iter; // iter++ also works but is somewhat less efficient }
Couldn't think of a better example but the idea is you add something to the list and because of post-increment, you still add the right iter, and then increment. It only saves a line but you rely on other programmers to know that which is dumb in my opinion.
|
On January 28 2015 04:19 Liebig wrote:Show nested quote +On January 28 2015 02:51 ZenithM wrote:On January 28 2015 02:37 Liebig wrote:On January 28 2015 01:43 ZenithM wrote:On January 28 2015 01:31 Liebig wrote:I'm proficient in none of them rofl Hmm, what do you program in? To me this list and the article didn't come as much of a surprise at all so that's why I was wondering why Manit0u found it interesting. Glad Scala is gaining ground, but it's still far from being mainstream. It's so powerful though, holy shit, you can't go back to Java after tasting t3h Scala... Mainly OCaml and Coq. Haha nice. "Cocorico!" as they say. You're probably from a similar educational background as mine ;D. I can't really picture someone dabbling in Coq in their free time, it's not something "natural" to stumble upon. I did prépa + ENS :o I'm probably going to start a phd related to Coq in a year or so, but yeah, prolly few people will ever play with Coq without some academic background But I find it kinda like a game where a theorem is your enemy and you try to kill it by proving it with tactics and such :o On the other hand, I have obviously touched mainstream languages like C, Java and such during my undergrads, but never used them again since then and I hope I never will :o Haha same, prépa + ENS, not unlikely we crossed path at some point ;D
Also, note to everyone, never say, like me, "I dabble in Coq in my free time".
|
On January 28 2015 07:18 Z-BosoN wrote:Show nested quote +On January 28 2015 07:13 solidbebe wrote: btw is there a difference between ++i and i++? ++i updates i, then does whatever, while i++ does whatever, then updates i i = 9 ++i == 9 is FALSE, because i incremented before checking the equality i++ == 9 is TRUE, because the original value was used in the equality before getting incremented in both instances i is 10 after the increment
this is not quite right in the context of c++, let me provide some clarity...
regarding ++i == j and i++ == j:
in both cases i is incremented before the equality comparison is made because both suffix- and prefix-increment operators have a higher precedence than ==. however the return values of the two increment operators, which is used as the left hand side argument of the == operator, might differ. presumably ++i increments i then returns a reference to i, while i++ makes a copy of i, increments i, then returns said copy (this copy could concern efficiency).
thus in the first case a reference to the incremented i is passed to the == operator, but in the latter case a copy of non-incremented i is passed to the == operator.
the differing returns of the prefix and suffix operator can be readily demonstrated with a small example, wherein decltype(auto) is used to make the type of left hand side of an assignement mirror the return type of the right hand side expression, and the type trait is_reference is used to deduce whether the types of j and k are references or not.
+ Show Spoiler [main.cpp] +#include<iostream> #include<type_traits> using namespace std;
int main() { int i=0; decltype(auto) j=++i; decltype(auto) k=i++;
cout<<boolalpha <<"j is reference to int: "<<is_reference<decltype(j)>::value<<endl <<"k is reference to int: "<<is_reference<decltype(k)>::value<<endl; } + Show Spoiler [output] +[jeh@gimli tl]$ ./tl j is reference to int: true k is reference to int: false
regarding ++i > j and (i+=1) > j:
since i+=k presumably adds k to i and returns a reference to i, when k=1, ++i > j and (i+=1) > j are equivalent... prefer the first one, apparently it's an excellent noob detector.
|
How is it a noob detector when all it does is waste people's time at work to find out what's really going on? Btw, I know you're obsessed with C++11/14 and that I may not be familiar with all the new features, but your code is unreadable because you abuse all kind of new shit. For example, decltype(auto). Do you write such code at work if you work?
Edit: It doesn't even compile in Visual Studio 2012, so you have to use a C++14 compiler.
As an alternative:
int i = 0; int& j = ++i; int k = i++;
|
Canada8025 Posts
Is there an actual benefit to using ++i at a high level?
The only time I've ever seen it used is in C code intended for microprocessors. Basically, the device had hardware that allowed you to increment pointers while simultaneously reading values from memory, the end result being that you could optimize the hell out of loops.
|
when what is really going on is a fundamental aspect of the language you are writing in, such as operators and their precedence, you ought to spend some time to figure that out.
that's why i clearly stated what i used decltype(auto) for. yes, i write as cool code as i can. if i didn't i would lose my passion.
whenever i find stuff i don't understand it usually means that my colleagues can teach me something i didn't know, and luckily i enjoy learning. in that sense this expression seems to be a noob detector.
edit: you are missing the point. in my demonstration gcc proves me right. if i used your alternative, i would just restate what i already said.
|
On January 28 2015 09:45 Spazer wrote: Is there an actual benefit to using ++i at a high level?
The only time I've ever seen it used is in C code intended for microprocessors. Basically, the device had hardware that allowed you to increment pointers while simultaneously reading values from memory, the end result being that you could optimize the hell out of loops.
So the short answer in a language like C++ seems to be: it depends how expensive the copy constructor is.
http://stackoverflow.com/a/24904
|
Haha yeah nunez does seem to tend to go overboard with new C++ features. In a work setting it's definitely a priority that your shit is readable so I wouldn't really abuse the so-called "noob detectors". I mean, let's be realistic here, most of the time, you'll work with noobs anyway. :O
|
@zenith i will not sacrifice my righteous passion at the altar of greed and profit, nor will i retard my rapture into the realms of the holy binary on account of another mans acrophobia.
@spazer if the two are otherwise equivalent, then the one that might give better performance sounds like a good default.
|
I wouldn't have it any other way, nunez, I like reading your posts, all the more so as I'm not super on point with C++ newest stuff :D
|
Hey guys, I was having some trouble creating a list with my array, if i could get some help on what i need to do to fix it it would be greatly appriciated.
my array:
$images = array(); $images[22] = array("id"=>22,"title" => "View of Cologne", "description" => "View of Cologne from atop the Cologne Cathedral", "country" => "Germany", "user" => "João Fernandes", "path" => "6114850721.jpg"); $images[54] = array("id"=>54,"title" => "Arch of Septimus Severus", "description" => "In the Roman Forum", "country" => "Italy", "user" => "Ellie Sullivan", "path" => "9495574327.jpg"); $images[7] = array("id"=>7,"title" => "Lunenburg Port", "description" => "On board a small sailing ship leaving Lunenburg", "country" => "Canada", "user" => "Mark Taylor", "path" => "5856697109.jpg"); $images[19] = array("id"=>19,"title" => "British Museum", "description" => "The library in the British Museum in London", "country" => "United Kingdom", "user" => "Mark Taylor", "path" => "5855729828.jpg");
$images[46] = array("id"=>46,"title" => "Temple of Hephaistos", "description" => "Located on western perimeter of Agora in Athens. Built in 460-415 BCE, it is the best preserved temple of antiquity.", "country" => "Greece", "user" => "Ellie Sullivan", "path" => "8711645510.jpg"); $images[6] = array("id"=>6,"title" => "At the top of Sulpher Mountain", "description" => "At top of Sulpher Mountain near Banff", "country" => "Canada", "user" => "Frantisek Wichterlová", "path" => "6114907897.jpg");
and my code to try to access the path of each index into a image link
<?php foreach($images as $key => $value) { echo '<a href ="travel-image.php"><img src="images/travel/square/' . $value['path'] . '"></a>'; } ?>
now the problem im having trouble undererstanding is how to link each image to a different page with its respective other keys($title, $country, $description, $user)
my template for each page is
<div class="row"> <div class="col-md-8"> image here </div> <div class="col-md-4"> <div class="panel panel-primary"> <div class="panel-body"> <p>By <a href="#">user here</a></p> <p>Taken in <a href="#">country here</a></p> <p>description here</p> </div> </div> </div>
thank you in advance for your advice
|
|
That is a very nice example nesserev. And yeah I think thats a pretty cool idea, Im up for it !
|
On January 28 2015 10:58 dapierow wrote:Hey guys, I was having some trouble creating a list with my array, if i could get some help on what i need to do to fix it it would be greatly appriciated. my array: $images = array(); $images[22] = array("id"=>22,"title" => "View of Cologne", "description" => "View of Cologne from atop the Cologne Cathedral", "country" => "Germany", "user" => "João Fernandes", "path" => "6114850721.jpg"); $images[54] = array("id"=>54,"title" => "Arch of Septimus Severus", "description" => "In the Roman Forum", "country" => "Italy", "user" => "Ellie Sullivan", "path" => "9495574327.jpg"); $images[7] = array("id"=>7,"title" => "Lunenburg Port", "description" => "On board a small sailing ship leaving Lunenburg", "country" => "Canada", "user" => "Mark Taylor", "path" => "5856697109.jpg"); $images[19] = array("id"=>19,"title" => "British Museum", "description" => "The library in the British Museum in London", "country" => "United Kingdom", "user" => "Mark Taylor", "path" => "5855729828.jpg");
$images[46] = array("id"=>46,"title" => "Temple of Hephaistos", "description" => "Located on western perimeter of Agora in Athens. Built in 460-415 BCE, it is the best preserved temple of antiquity.", "country" => "Greece", "user" => "Ellie Sullivan", "path" => "8711645510.jpg"); $images[6] = array("id"=>6,"title" => "At the top of Sulpher Mountain", "description" => "At top of Sulpher Mountain near Banff", "country" => "Canada", "user" => "Frantisek Wichterlová", "path" => "6114907897.jpg");
and my code to try to access the path of each index into a image link <?php foreach($images as $key => $value) { echo '<a href ="travel-image.php"><img src="images/travel/square/' . $value['path'] . '"></a>'; } ?>
now the problem im having trouble undererstanding is how to link each image to a different page with its respective other keys($title, $country, $description, $user) my template for each page is
<div class="row"> <div class="col-md-8"> image here </div> <div class="col-md-4"> <div class="panel panel-primary"> <div class="panel-body"> <p>By <a href="#">user here</a></p> <p>Taken in <a href="#">country here</a></p> <p>description here</p> </div> </div> </div>
thank you in advance for your advice
You don't use arrays for that. You have relational databases for this purpose.
Edit:
I'll cave...
{% if images is iterable %} {% for image in images %} <div class="row"> <div class="col-md-8"> <a href="travel-image.php"> <img src="{{ 'images/travel/square/' ~ image['path'] }}" alt="{{ image['title'] }}"> </a> </div> <div class="col-md-4">
<div class="panel panel-primary"> <div class="panel-body"> <p>By <a href="#">{{ image['user'] }}</a></p> <p>Taken in <a href="#">{{ image['country'] }}</a></p> <p>{{ image['description'] }}</p> </div> </div>
</div> </div> {% endfor %} {% endif %}
Is that what you wanted? (used Twig for brevity and readability)
That will display all the images. If you want the page of a single image then you must pass additional parameters to your single image view. Example in raw php this time:
// page with images <a href="travel_image.php?id=<?php echo image['id'] ?>" ...
// single image page $id = $_GET['id']; $image = $images[$id];
I didn't think anyone would write pages in raw php without mvc and/or some framework this days...
|
On January 28 2015 16:20 Nesserev wrote:Nunez, I think the most clear way to show the difference between ++i and i++, is by just showing their typical definitions for a class Foo, with one member called 'value'. I'm pretty sure that in a lot of cases, i++ is optimized to the same effect as ++i by the compiler. Foo& Foo::operator++() // called for ++i { this->value += 1; return *this; // reference is returned }
// the ignored int parameter is used to distinguish ++i and i++, for any class
Foo Foo::operator++(int) // called for i++ { Foo copy(*this); // copy constructor is called to make a copy of this object ++(*this); return copy; // copy is returned }
sidenote: your previous alternative should also demonstrate that attempting to bind the result of the suffix increment expression to a reference does not compile.
frontnote: showing a case of canonical operator overloading for a contrived class on an imaginary type is good, but i demand more rigour in the general case (as we have not revealed anything about the type of value, or even the use of Foo), calling the expected operators on the held value:
Foo Foo::operator++(int) { return Foo(value++); }
Foo& Foo::operator++() { ++value; return *this; } however there is something left unsaid about the increment operators of the value and their exception specifications, it is not something the holy binary would compile, the restrictive and seemingly random nature of the dummy int variable is an abomhination to good taste and rational thought, and it is not as elegant or fundamental as showing it's the case for a primitive type...
behold this proof by compilation (i noobed the other one down a bit):
#include<type_traits> int main() { int i; static_assert( !std::is_reference<decltype(i++)>::value&& std::is_reference<decltype(++i)>::value, "!QED" ); } what glorious code! most precise! praise the GCC! halleluja!
the existence of situations where the performance differs twixt prefix and suffix is enough to tip the scales in favor of the prefix increment over the suffix increment as the reasonable default. specifically what situations is interesting, but not of consequence, when they are otherwise equivalent.
have pity on your loving mothers, for they will not know how to begin forgiving you for even suggesting to use add-assign in the place of increment.
backnote: a little entertainement, canonical implementation of the rvalue reference qualified suffix increment operator:
Foo operator++(int) && { return std::move(*this); }
|
And here all I do is just C, C# and the occasional Java when it's unavoidable. I seem to be missing out on so much
|
|
On January 29 2015 02:18 Manit0u wrote:Show nested quote +On January 29 2015 02:08 bangsholt wrote:And here all I do is just C, C# and the occasional Java when it's unavoidable. I seem to be missing out on so much You're not. C++ is scrapcode. Nothing like glorious C or elegant C# If you want truly elegant metaprogramming you need to look no further than Lisp, which is ahead of any other programming language since 1958
I disagree. C++ is really cool but some people do their best to make their code unreadable. "Glorious C" - good luck with that unsafe language. :D
|
On January 29 2015 03:56 darkness wrote:Show nested quote +On January 29 2015 02:18 Manit0u wrote:On January 29 2015 02:08 bangsholt wrote:And here all I do is just C, C# and the occasional Java when it's unavoidable. I seem to be missing out on so much You're not. C++ is scrapcode. Nothing like glorious C or elegant C# If you want truly elegant metaprogramming you need to look no further than Lisp, which is ahead of any other programming language since 1958 I disagree. C++ is really cool but some people do their best to make their code unreadable. "Glorious C" - good luck with that unsafe language. :D
I'm interpolating a bit here, but are you trying to say that C is unsafe and C++ is... safe?
|
On January 29 2015 04:03 bangsholt wrote:Show nested quote +On January 29 2015 03:56 darkness wrote:On January 29 2015 02:18 Manit0u wrote:On January 29 2015 02:08 bangsholt wrote:And here all I do is just C, C# and the occasional Java when it's unavoidable. I seem to be missing out on so much You're not. C++ is scrapcode. Nothing like glorious C or elegant C# If you want truly elegant metaprogramming you need to look no further than Lisp, which is ahead of any other programming language since 1958 I disagree. C++ is really cool but some people do their best to make their code unreadable. "Glorious C" - good luck with that unsafe language. :D I'm interpolating a bit here, but are you trying to say that C is unsafe and C++ is... safe?
Well, I've recently started learning C++ and I can say it looks safer so far. Provided that you use the right tools, of course.
Edit: C's macros immediately come to mind.
I'm also not saying C++ is completely safe, it obviously has a lot of undefined behaviour (e.g. dangling raw pointers) but I think it's safer than C.
|
|
|
|