×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / (C++)How to get object from vector one by one in a loop? thanks. After I erase an element from a list,where the pointer will point to?
    • 再详细点,第二个问题pointer是指哪一个?
    • template
      use iterator:
      vector<object>::iterator aIterator;

      for (aIterator = aVector.begin();aIterator != aVector.end();aIterator++){
      // loop
      }

      the statement to remove an element from a vector is:
      aIterator2 = aVector.erase(aIterator1);
      aIterator2 is an iterator which desigates the next available element.
      • in my interview, a question is :what's wrong
        for (aIterator = aVector.begin();aIterator != aVector.end())
        {
        if (aVector.get(aIterator )==0)
        aIterator =aVector.erase(aIterator );
        alterator++;

        }

        so, if the last element is 0,error ?I failed to answer this question
        • if alterator points to the last element, aVector.erase(aIterator ) will return end().
          So you have to check if alterator is at the end. Otherwise you will get an error. The modified code is as follows,


          for (aIterator = aVector.begin();aIterator != aVector.end())
          {
          if (aVector.get(aIterator )==0)
          aIterator =aVector.erase(aIterator );
          if (aIterator != aVector.end()) alterator++;

          }
          • It seems that you are an expert in C++, could you please help me? Just some simple questions.Thank you!
            本文发表在 rolia.net 枫下论坛I installed VC++6.0 this morning and tried the program. But it still had some problems:
            1, I want to know a function that can read the current time of the system, and how to use it. Is it read the seconds or the minutes?In C, it is called biostime().
            2, I built the .exe file and I ran it. The program first just read some parameters as you input them, and also the .dat file name. Please see below:

            1 printf(" Please input learning factor---Et & dEt: (
            Note:0.1>=Et>=0) \n");
            2 scanf("%f",&Et);
            3 scanf("%f",&dEt);

            4 printf(" Please slope value of g() --- alfa:\n");
            5 scanf("%f",&alfa);
            6 printf(" Please input learning factor---beita:\n");
            7 scanf("%f",&beita);
            8 printf(" Please input learning factor---bstep:\n");
            9 printf(" ( Note: 0.1beita>bstep>0.000 ) \n");
            10 scanf("%f",&bstep);

            11 gets(ldfnm);
            12 printf(" Please input learning data file name(iol?.dat):\n");
            13 gets(ldfnm);

            14 sss=fopen(ldfnm,"r");
            15 fscanf(sss,"%d%d",&inputN1,&inputN1);
            16 fscanf(sss,"%d",&modelN);
            17 outputN=62;
            18 position=ftell(sss);
            19 p=position;
            20 fclose(sss);

            So the above part works until line 13, I can input all those parameters. But when I input the .dat file name (line 12 stll works), it stop working. A window jump out and said that
            "Debug assertion failure.
            Program: D:\program\Debug\learning.exe
            File: fscanf.c
            Line: 54
            expression: streams!=NULL"

            Could you please tell what's wrong with the program? What means assertion failure?How to deal with it?
            Thank you very much!更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • Several ways can be used to get the current time
              1. You can try GetSystemTime() as follows:

              SYSTEMTIME currentTime;
              int second, minute;
              GetSystemTime(&currentTime);

              second = currentTime.wSecond;
              minute = currentTime.wMinute;
              .......

              2. I don't know how you define the string ldfnm. You should define char ldfnm[30], or something like that, to allocate memory. Otherwise you may get an error.
              Assertion is a debug method. The statement is like : assert(x==1). if x does not equal to 1, the program will raise an assertion failure. It can help you to debug the program. There are a lot of assertion statement in the C/C++ library to help you debug your programm.
              • Thank you very much and I have sloved the problems.
            • There are several ways to get the current time
              1. You can try GetSystemTime() as follows:

              SYSTEMTIME currentTime;
              int second, minute;
              GetSystemTime(&currentTime);

              second = currentTime.wSecond;
              minute = currentTime.wMinute;
              .......

              2. I don't know how you define the string ldfnm. You should define char ldfnm[30], or something like that, to allocate memory. Otherwise you may get an error.
              Assertion is a debug method. The statement is like : assert(x==1). if x does not equal to 1, the program will raise an assertion failure. It can help you to debug the program. There are a lot of assertion statement in the C/C++ library to help you debug your programm.
          • good,good, thanks,thanks.
          • thanks. u r so helpful