stereomatching 17 Posting Whiz in Training

I don't really get your question
you could save all of the string into a vector rather
than save them into an old style array
Then iterate through all of the string in the vector

//don't pass by value but pass by reference
//remember to add const if you don't want the str to be changed
int numS( string str )
{
    int countChar = 0;
    
    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == 's' )
            countChar++;
    }
    
    return countChar;
}

you could make your life easier by template

template<char ch>
size_t num( std::string const &str )
{
    int countChar = 0;

    for (int i = 0; str[i] != '\0'; i++)
    {
        if ( str[i] == ch )
            countChar++;
    }

    return countChar;
}

typedef num<'z'> numZ;
typedef num<'s'> numS;
typedef num<'c'> numC;

I don't know why are you want to design something like numA, numZ and so on
would it better than countChar?

stereomatching 17 Posting Whiz in Training

I have to count how many c,s,z are there in that array. I did it like this and i can't figure it out... Thanks again for help...

you mean your countChar has some problem?
Your logics looks good for me, but you have to pass the std::string like this

size_t countChar( vrstica.c_str() , 's' );

and change the interface to

size_t countChar( char const *str , char ch );

besides, you don't need to pass the string by array and write a for loop
you could just do it like this

void reverseString(string const &a)
{    
    string rstr = a;
    reverse(rstr.begin(), rstr.end());
    cout << rstr << endl;    
}

There are many ways to do the same job, you could try another solutions if you like
example : make the codes become shorter and save a temporary variable i

size_t countChar( char const *str , char ch )
{
    size_t countChar = 0;

    //generally speaking, postfix++ is not a good idea, but it
    //is ok for primitive types like char
    while(*str) if(*str++ == ch) countChar += 1;          

    return countChar;
}

you could solve this problem by stl too

template<typename ForwardItr>
size_t countChar( ForwardItr begin , ForwardItr end, char ch)
{
  size_t sum = 0;  
  std::for_each(begin, end, [&](char fir){ if(fir == ch) ++sum;} );

  return sum;
}
stereomatching 17 Posting Whiz in Training

Your codes are not bad, but there are still something could be refine

//this is not initialize but assignment
Player::Player(int MyID, string MyFactionName) {
  ID=MyID;
  FactionName=MyFactionName;
}
//This is initialize
Player::Player(int MyID, string MyFactionName) :ID(MyID), FactionName(MyFactionName){ }
//you are intent to copy the string, use std::string const& instead of string
//if you want to construct MyFactionName directly, it is another story
Player::Player(int MyID, string MyFactionName) {
  ID=MyID;
  FactionName=MyFactionName;
}
//you could save the size into another variable
//size_t Size = MyUnits.size();
//use size_t rather than unsigned int, it would be easier to shift to 64bits
//use prefix++ rather than postfix++
//use != rather than < if you want to cope with generic programming
for(size_t i=0; i != Size; ++i) {

just some opinions of coding style, another problems like protected or the name
like a, b, c, d, e.Better don't do these in a big project or when you need to
cooperate with other.

If my comments have any mistakes, please feel free to critics me.Thanks

stereomatching 17 Posting Whiz in Training

another solution

#include<algorithm>
#include<iostream>
#include<iterator>
#include<string>

template<typename ForwardItr>
void reverseString(ForwardItr begin, ForwardItr end)
{
  while(begin != end)
  {
    std::reverse(begin->begin(), begin->end());
    ++begin;
  }
}

int main()
{
  std::string value[] = {"one", "two", "three"};
  enum {Size = sizeof(value)/ sizeof(*value)};
  reverseString(value, value +  Size);
  std::copy(value, value + Size, std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}

you could place your std::string into std::vector or std::list either

#include<algorithm>
#include<iostream>
#include<iterator>
#include<string>
#include<vector>

template<typename ForwardItr>
void reverseString(ForwardItr begin, ForwardItr end)
{
  while(begin != end)
  {
    std::reverse(begin->begin(), begin->end());
    ++begin;
  }
}

int main()
{
  char const *str[] = {"one", "two", "three"};
  std::vector<std::string> value(str, str + sizeof(str) / sizeof(*str) );
  reverseString(value.begin(), value.end());
  std::copy(value.begin(), value.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}
stereomatching 17 Posting Whiz in Training

compile by vc2010, coding style of C++11?

#include<algorithm> //for std::for_each, std::reverse and std::copy
#include<array> //for std::array
#include<iostream> //for std::cout, std::cin.get
#include<iterator> //for std::ostream_iterator
#include<string>

template<typename T>
void reverseString(T &value)
{  
  std::for_each(value.begin(), value.end(),
  [](std::string &fir)
  {
    std::reverse(fir.begin(), fir.end());
  });
}

int main()
{    
  std::array<std::string, 3> value = {"one", "two", "three"};
  reverseString(value);
  std::copy(value.begin(), value.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;
}

compile by vc2010, coding style of C++98?

#include<algorithm>
#include<iostream>
#include<string>

//I don't know how to deduce the number of the element at compile time
//without passing a parameter "num", please tell me how to
//do it if you know, thanks
void reverseString(std::string value[], size_t num)
{
  for(size_t i = 0; i != num; ++i)
    std::reverse(value[i].begin(), value[i].end() );
}

int main()
{    
  std::string value[] = {"one", "two", "three"};
  reverseString(value, sizeof(value)/ sizeof(*value) );
  std::copy(value, value + 3, std::ostream_iterator<std::string>(std::cout,"\n"));
  std::cin.get();

  return 0;  
}

the implementation of std::reverse are quite simple
I only post the one with random access iterator(a smart pointer which similar to pointer)

template<typename RandomItr>
void Reverse(RandomItr begin, RandomItr end)
{
  while(begin < end)
  {
    std::iter_swap(begin, --end);
    ++begin;
  }
}
stereomatching 17 Posting Whiz in Training

Here's a link to a read me thread in the Computer Science forum

The opinions are pretty good, but there are some things about C++
I don't agree with.

>C++ is an Object Oriented Programming (or OOP)
C++ is an multi-paradigms language

>Programs can be slightly larger and slower than those programmed in C
This is same as C, since C++ could generate same binary codes as C
The quality of the programs are depend on the coders

The mysteries about C++ is an OO language and higher abstraction will(not "may")
make the codes become slower and fatter are always lingering within
the world of programming.At first I thought this is because the other
tools don't have a "Turing completeness" macro and template like C++ do.
But recently, I find out that even many programmers of C++ believe in this
kind of mysteries.

C++ and a lot of techniques(not only template) already prove that "higher
abstraction do not equal to slower, fatter codes" many years, yet there are
still many programmers(even some professors of colleges) still believe
these kind of myths, how could this happen?

stereomatching 17 Posting Whiz in Training

or boost tokenizer

std::string mid_str("I have a dog");
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
tokenizer tokens(mid_str, boost::char_separator<char> (" "));
std::string strArray[4];
std::copy(tokens.begin(), tokens.end(), strArray);
stereomatching 17 Posting Whiz in Training

This one is much more better

std::list<size_t> A = { 1, 2, 3, 4, 2, 1, 6, 9 };
  auto it = A.begin();
  while( it != A.end() )
  {
    it = std::adjacent_find(it, A.end(), [](size_t const Fir, size_t const Sec)
    {
      return Sec <= Fir;
    } );
    if( it != A.end() )
    it = A.erase(it);
  }
  std::copy(A.begin(), A.end(), std::ostream_iterator<size_t>(std::cout, ", ") );

you could change your requirement of < or <=, dependent on your need.
STL is just like lego, your question is a good lesson for me too
Thanks a lot

refinement of my previous code

//turn all of the return last; to return;
void remove_adjacent(size_t *first, size_t *last, std::vector<size_t> &result);

ps : most of the compilers don't support the initialization way of { 1, 2, 3, 4, 2, 1, 6, 9 } yet
maybe this is because they lack variadic template(I guess)?

stereomatching 17 Posting Whiz in Training

key word : std::greater
if std::greater can't meet your need, you could try
key words : functor, lambda expression, std::function, std::bind

stereomatching 17 Posting Whiz in Training

you could tune the codes to make it become safer, faster and generic
as your need, if there are bugs, please tell me, I will try my best to
fix it

size_t *remove_adjacent(size_t *first, size_t *last, std::vector<size_t> &result)
{
  if(first == last) return 0;

  size_t *next = first + 1;
  if(next == last) result.push_back(*first);
  while(next != last)
  {
    if(*first <= *next)
    {
      std::cout<<*first<<", "<<*next<<std::endl;

      result.push_back(*first);
      ++first; ++next;
      if(next == last)
      result.push_back(*first);
    }
    else if(*first > *next)
    {
      std::cout<<*first<<", "<<*next<<std::endl;
      result.push_back(*first);
      if(last - next > 1)
      {
        first += 2;
        next  += 2;
      }
      else
      {
        return last;
      }
    }
  }
}

void case_03()
{
  size_t A[] = { 1, 2, 3, 4, 2, 1, 6, 9 };
  std::vector<size_t> result;
  remove_adjacent(A, A + sizeof(A) / sizeof(*A), result);
  std::copy(result.begin(), result.end(), std::ostream_iterator<size_t>(std::cout, "\n") );
}
WaltP commented: That would be good. Then he doesn't have to do the project himself and he can pass his class on YOUR work. -4
Stefano Mtangoo commented: Why would you do his work? Why??? -3
stereomatching 17 Posting Whiz in Training

simplest way

size_t A[] = {1, 1, 2, 3, 4, 4, 5};
size_t *APtr = std::unique(A, A + sizeof_array(A) );
std::copy(A, APtr + 1, std::ostream_iterator<size_t>(std::cout, "\n") );

or

//from cplus_plus.com
size_t *unique_test( size_t *first, size_t *last )
{
  size_t *result = first;
  while (++first != last)
  {
    if (!(*result == *first))
      *(++result)=*first;
  }
  return ++result;
}

size_t A[] = {1, 1, 2, 3, 4, 4, 5};
size_t *APtr = unique_test(A, A + sizeof_array(A) );
std::copy(A, APtr + 1, std::ostream_iterator<size_t>(std::cout, "\n") );
stereomatching 17 Posting Whiz in Training

looks like you need to read a file(like txt?)
If you want to read a file, you need std::ifstream or std::fstream
but I would suggest for std::ifstream.

struct student_info
{
  size_t id;
  std::string name;
  size_t subject_num;
  //you could consider another containers according to your need
  std::map<std::string, size_t> subCode_and_marks;
  
  private :
    //insert the data according to the number of the subject_num
    void insert_subCode_and_marks();
};

There are a lot of the ways to solve this problem
I don't know is this what you want?
std::map could extend the sizes dynamically
if you want to save them as dynamic array
you could try

std::vector<std::pair<std::string, size_t> > subCode_and_marks;

I hope this could help you

stereomatching 17 Posting Whiz in Training

I don't know what are you trying to do
But a dynamic struct within a struct is pretty easy

struct student_info
{
  std::string name;
  size_t id;
  .......and so on
};

class student_handle
{
  public : 
  //define the function you want
  
  private :
    std::vector<student_info> data_;//dynamic array of struct student_info
};

you could make the codes become more generic if you need

stereomatching 17 Posting Whiz in Training

the other way than vector<int>(not recommend)

int *setLength(int *myArray, int length)
{
  if(myArray)
  delete[] myArray;

  myArray = new int[length];
  return myArray;
}

int main()
{
  int *myArray = new int[5];
  myArray = setLength(myArray, 10);

  delete []myArray;  

  return 0;
}

my conclusion
use vector instead of raw pointer
you could learn pointer step by step, don't need
to be hasty

stereomatching 17 Posting Whiz in Training

your code doesn't looks like the c++ I familiar with
where is your "resize" come from?
and what is ref?

Array.Resize(ref myArray, length);

besides,

int[] myArray = new int[4];

is not the way to declare one dimension array
you should use

int *myArray = new int[4];

Here is the simplest way to do it what I know fow now

#include<vector>

void setLength(std::vector<int> &myArray, int length)
{
  myArray.resize(length);
}

int main()
{
  std::vector<int> myArray;
  setLength(myArray, 10);

  return 0;
}

besides, c++ only allow one main
what is the meaning to add static infront of normal function(or they are member function?)

exactly, with vector you don't need to design setLength
just call ".resize()"

stereomatching 17 Posting Whiz in Training
srand

you need to provide a seed

stereomatching 17 Posting Whiz in Training

the break would kick you out of the nearest loop

while (num=(rand() %500));

should not put ";"

you could just use "else" instead of "else if" if that is the last option

else (guess==num){cout<< "You got it!"; break;}
while (num=(rand() %500))

this line has a small problem since num may become 0
if num == 0, you would never go into the loop

you are almost there, good luck

stereomatching 17 Posting Whiz in Training

you could wrap it in one while loop
you may use if...else and break to solve this question
a simple example

while(1)
{
  if(num < xx) .....
  ......
  else if(num == yy) break;
}
stereomatching 17 Posting Whiz in Training

you may want to write your code in such a way that this issue is addressed.

I would rather use type_traits to avoid the problem of code bloat.
type_traits is ease to use, although the syntax is a little bit verbose