2d vectors

Pages: 12
i was thinking auto was a c++11 thing and for now i want to stay away from c++11 I still have to master c++98 :D :D :D i still have to learn the auto things though...
ok am going to omit the * was thinking since i wanted to delete the value from the vector i had to place * thanks DTSCode..
closed account (Dy7SLyTq)
sorry didnt know you wanted to stay away from 11. then the only important part is yeah, you omit the *, because the iterator is already a pointer so your passing a ** to a *
donvigor wrote:
i want to stay away from c++11 I still have to master c++98

What's the point of mastering 15-year old technology? Do you plan to travel back in time?


std::vector<int>::iterator itr1, itr2 = container.end();

for (itr1=container.begin(); itr1 != itr2; ++itr)
{
if(*itr!==temp) container.erase(*itr);
}

After changing itr1 to itr, !== to == (if that was your intention) and *itr to itr, this would compile, but would still be wrong: after you execute container.erase(itr), that itr is no longer valid. ++itr, which your loop executes next, is undefined. Note that vector::erase() returns a new, valid, iterator. use it to replace the old trashed itr (and don't increment it in that case). Or just use erase-remove as intended by C++98 (it's also much faster than your loop after fixes)

container.erase( remove(container.begin(), container.end(), temp), container.end() ):
Last edited on

INFACT! what am doing is not working! getting me sooo frustrated!
about your point Note that vector::erase() returns a new, valid, iterator. use it to replace the old trashed itr (and don't increment it in that case) so how is that done? now lemme correct the code up there...so you do an example for me....so i can check on it and learn...you've been very great Sir!!!
I actually meant this...
1
2
3
4
5
6
7
std::vector<int>::iterator itr1, itr2 = container.end();

for (itr1=container.begin(); itr1 != itr2; ++itr)
{ 
if(*itr1==temp) container.erase(itr);
}
 

sorry that i dont understand right away and i usually need some more explanation... in the example you gave..
container.erase(remove(container.begin(), container.end(), temp), container.end() );
I dont want to eliminate temp...i want to delete the value that itr1 is pointing to...which is equal to temp...am happy you are teaching me new great things...
but can you please put your example in the example i gave so i understand it more clearly ? thanks

so pls you do an example with my short code with all the two methods you mentioned...so i can compare and comprehend more..I hope am not asking too much Sir Cubbi..thanks...
pls you do an example with my short code with all the two methods you mentioned.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> container = {1,2,3,4,3,2,1,2,3,4};

    // output
    std::cout << "Before: ";
    for(int n: container) std::cout << n << ' ';
    std::cout << '\n';
    
    // erase every 2 using erase-remove
    int temp = 2;
    container.erase( std::remove(container.begin(), container.end(), temp),
                     container.end() );
    
    // output
    std::cout << "After erasing 2: ";
    for(int n: container) std::cout << n << ' ';
    std::cout << '\n';

    // same thing (but slower) with a loop
    temp = 3; // erase every 3
    std::vector<int>::iterator itr1, itr2 = container.end();
    for (itr1=container.begin(); itr1 != itr2; )
    { 
        if(*itr1==temp)
            itr1 = container.erase(itr1);
        else
            ++itr1;
    }

    // output
    std::cout << "After erasing 3: ";
    for(int n: container) std::cout << n << ' ';
    std::cout << '\n';
}

online live demo: http://ideone.com/q1G2VX
Thanks Sir! for (int n: container) ...is that a c++11 move? I've never come across a for loop like that....anyway thank you very much Sir Cubbi...been on this thing for hours now...am gonna rest now and continue tomorrow..is very late here at my end..am in italy..and i guess you guys are in the US...thank you very much again...but one last thing...how do i change my my codeblocks compiler to that of c++11 compiler? thanks for all your troubles..catch you tomorrow...Jah bless!
closed account (Dy7SLyTq)
yes its the c++11 range based for loop
What's the point of mastering 15-year old technology? Do you plan to travel back in time?

If you're learning C++ at home for a hobby, then, no, there's no point.

But I can tell you that in industry, there are a lot of companies who haven't yet migrated to development environments that support C++11. Companies often have ongoing projects that have been around for years, and will stick to operating systems, compilers and so on that have been thoroughly validated and approved by their procedures. Individuals are not at liberty to simply switch to newer versions of the OS or compiler, and must work using the approved tools.

The process of validating and approving newer versions can be time-consuming and expensive, and the developers will usually have a ton of other work to be getting on with. Many developers - and managers - will work under a principle of "if it ain't broke, don't fix it. When a company's success depends on producing work in a timely fashion, I can't say I really blame them.

Additionally, many companies will have a set of established coding guidelines and practices. C++98 has been around for a long time, and commonly-understood ideas of "good practice" are widespread and mature. With the features of the new standard, this is less well-understood, and many companies will be reluctant to make the change to C++11 without first being confident they can specify good guidelines for the developers.

In short, then - if you want to work as a professional C++ developer, there's a very good chance you'll be working in an environment where you can't use C++11 features. So it will set you in good stead to learn how to program well, and implement the behaviour you want to implement, without using C++11 features.
all the places I worked at used, relied heavily, and on a few occasions invented the practices and solutions that became so popular that they were later standardized as C++11, years, almost decades ago. Starting out with current C++ gives understanding of these practices even if they are implemented in legacy compilers in many places today. Setting yourself on the outset for dead-end jobs frozen in last century (how can they remain competitive?) isn't something I'd recommend.
Setting yourself on the outset for dead-end jobs frozen in last century (how can they remain competitive?) isn't something I'd recommend.

They remain competitive because, frankly, it's perfectly possible to develop software productively and efficiently without using the C++11 features. C++11 has some nice improvements over the previous standards, but it's hardly essential for software development. If it was, then previous versions of C++ would never have become as ubiquitous as they have.

In any case, I'm not arguing the rights and wrongs of rapid adoption of new standards. I am simply providing factual information about the realities of the professional software industry as it is today. Yes, there are companies that have adopted C++11, such as the ones you've worked for. But there also are a great many who haven't. If you start a C++ development job today, in August 2013, there is a very good chance you'll be using C++98, not C++11 - regardless of whether you think that should be the case.

Which is not to say it's not useful to learn and practice using C++11 features. Clearly, the more versatile you are as a developer, the more different jobs you'll have a chance of getting. But part of that versatility is being able to work with only the C++98 language at your disposal, so there is value in learning and practising programming with those features.
MikeyBoy wrote:
I am simply providing factual information about the realities of the professional software industry as it is today.

So am I.

Yes, there are companies that have adopted C++11, such as the ones you've worked for.

I didn't, that's not what I'm saying. I mostly worked for companies that use pre-C++11 compilers, but the code always relied on smart pointers, hash maps, enable_if and type traits, tuples and typelists, emulated move semantics, stateful allocators, threads (boost-style threads or sometimes just boost directly). atomics, alignment control, binds and lambdas (not always from boost, either), etc.

part of that versatility is being able to work with only the C++98 language at your disposal, so there is value in learning and practising programming with those features.

My point is that it doesn't make sense to start in the past and then try to play catch-up everywhere you go - I don't even consider hiring people who don't understand smart pointers. Start from today, then learn what's lacking and what's used instead as needed.
You guys have given me some great enlightenment with this constructive discussion...thanks everyone :D
Topic archived. No new replies allowed.
Pages: 12