You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


DEQUE'S AND STRING'S in C++
03-13-2016, 10:12 PM (This post was last modified: 03-13-2016 11:49 PM by Waltersmind.)
Post: #1
 (Print Post)
DEQUE'S AND STRING'S in C++
Code Snippet: [Select]
// deque::operator[] example: reversing order
#include <iostream>
#include <stdio.h>
#include <deque>
#include <string>

using namespace std;

struct EnemyStruct {
    string Type;
    int Room;
    string Description;
} ;

int main ()
{
  deque<int> mydeque (10);   // 10 zero-initialized elements
  deque<int>::size_type sz = mydeque.size();
  deque<string> MyStrings (10);
  deque<EnemyStruct> Enemy (3);
  deque<EnemyStruct> Enemy2;

  int TestArray[5];

  TestArray[1] = 45;

  // THE FOLLOWING COMMENTED CODE BLOCK WORK
  /*

      Enemy[0].Type = "BUG";
      Enemy[0].Room = 24;
      Enemy[0].Description = "This is a bug";

      Enemy[1].Type = "BUG";
      Enemy[1].Room = 48;
      Enemy[1].Description = "This is another bug";

      Enemy[2].Type = "BUG";
      Enemy[2].Room = 112;
      Enemy[2].Description = "This is yet another bug";

  */

  Enemy[0] = {"BUG1", 24, "This is bug #1."};
  Enemy[1] = {"BUG2", 48, "This is bug #2."};
  Enemy[2] = {"BUG3", 112, "This is bug #3."};

  Enemy2.push_back({"BUG1-B", 2400, "This is bug #1-B."});
  Enemy2.push_back({"BUG2-B", 4800, "This is bug #2-B."});
  Enemy2.push_back({"BUG3-B", 11200, "This is bug #3-B."});

  MyStrings[0] = "This is a string #1.";
  MyStrings[1] = "This is a test.";
  MyStrings[2] = "This is only a test.";
  MyStrings[3] = "Maybe this is a test.";
  MyStrings[4] = "Is this a test?";
  MyStrings[5] = "I think it is a test.";
  MyStrings[6] = "I can't tell is it is a test.";
  MyStrings[7] = "Checking to see if it is a test.";
  MyStrings[8] = "This is a C++ example, so it must be a test.";
  MyStrings[9] = "Yep! This is a test of an array of deque strings.";

  cout << "TestArray[1]: " << TestArray[1] << "\n\n";

  for (unsigned i=0; i<Enemy.size(); i++)
  {
    cout << "Enemy.Type [" << i << "]: " << Enemy[i].Type << "\n";
    cout << "Enemy.Room [" << i << "]: " << Enemy[i].Room << "\n";
    cout << "Enemy.Description [" << i << "]: " << Enemy[i].Description << "\n\n";

  }

  for (unsigned i=0; i<Enemy2.size(); i++)
  {
    cout << "Enemy2.Type [" << i << "]: " << Enemy2[i].Type << "\n";
    cout << "Enemy2.Room [" << i << "]: " << Enemy2[i].Room << "\n";
    cout << "Enemy2.Description [" << i << "]: " << Enemy2[i].Description << "\n\n";

  }

  // assign some values:
  for (unsigned i=0; i<sz; i++) mydeque[i]=i;

  // reverse order of elements using operator[]:
  for (unsigned i=0; i<sz/2; i++)
  {
    int temp;
    temp = mydeque[sz-1-i];
    mydeque[sz-1-i]=mydeque[i];
    mydeque[i]=temp;
  }

   for (unsigned i=0; i<MyStrings.size(); i++)
   {
     cout << MyStrings[i] << "\n";
   }

   cout << "\n\n";

  for (unsigned i=0; i<MyStrings.size(); i++)
  {
      MyStrings[i] += " -> 3.141592653";
  }

  for (unsigned i=0; i<MyStrings.size(); i++)
  {
      cout << MyStrings[i] << "\n";
  }

  cout << "\n\n";


  // print content:
  cout << "mydeque contains:";

  for (unsigned i=0; i<sz; i++)
    cout << ' ' << mydeque[i];

  cout << '\n';

  getchar();

  return 0;
}

Please help support The Joyful Programmer and The QB64 Edition by visiting our online store provided by Amazon.com. We hand-picked books related to computer programming from Amazon.com and added them to our store. When you make a purchase from our store, we do receive a small commission from the sale. Visit our store at: http://www.thejoyfulprogrammer.com/qb64/...azon-store
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - OpenGL Triangles
QB64 Member Project - Spiro Roses
QB64 Member Project - Dreamy Clock
QB64 Member Project - Inside Moves
QB64 Member Project - 9 Board
QB64 Member Project - Overboard
QB64 Member Project - Rotating Background
QB64 Member Project - Swirl
QB64 Member Project - Full Color LED Sign
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Input
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - Color Rotating Text
QB64 Member Project - Color Triangles
QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Kings Valley verion one
QB64 Member Project - Bowditch curve
QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - Point Blank
QB64 Member Project - Score 4
QB64 Member Project - Touche
QB64 Member Project - Basic Dithering
QB64 Member Project - ARB Checkers
QB64 Member Project - Pivet version one
QB64 Member Project - Isolation
QB64 Member Project - Amazon
QB64 Member Project - Kings Vallery version two
QB64 Member Project - Pivot version two
QB64 Member Project - Domain
QB64 Member Project - Othello
QB64 Member Project - Quarto
QB64 Member Project - Algeria Weather
QB64 Member Project - Dakapo
QB64 Member Project - Splatter
QB64 Member Project - Foursight
QB64 Member Project - RGB Color Wheel
QB64 Member Project - Qubic
QB64 Member Project - Blokus
QB64 Member Project - Martin Fractals version two
QB64 Member Project - Rubix's Magic
QB64 Member Project - Sabotage
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Line Thickness
QB64 Member Project - Kings Court
QB64 Member Project - Connect Four
QB64 Member Project - Martin Fractals version three
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Exit