template<> 37 Junior Poster

typedef is handy when using stl containers to avoid typing. Read up on it. Below is a simple sample to help to compile, run, test and understand. Play around with it and when you understand whats going on, incorporate similar logic into your application.

Iterators are tricky and confusing at first, but its extensively used, so its worthwhile investment in time.

#include <iostream>
#include <vector>

int main( int argc, char *argv[])
{
    /// typedef lets us alias names
    typedef std::vector<std::string> TVec;

    /// below is a list of favarite dogs
    TVec v;
    v.push_back("jake");
    v.push_back("lucy");
    v.push_back("paws");

    std::cout << "Please enter name to remove:" << std::endl;
    std::string who;
    std::getline(std::cin,who);
 
    /// find and remove name on list
    bool found=false;
    for( TVec::iterator iter=v.begin(); iter!=v.end() && found==false; ++iter )
    {
        if( *iter == who )
        {
            v.erase(iter); // iterator passed to erase
            found=true; // exit the loop
        }
    }

    /// inform user of status
    if(found == true )
    {
        std::cout << who << " is removed" << std::endl;
    }
    else
    {
        std::cout << who << " is not on list" << std::endl;
    }

    return 0;
}
template<> 37 Junior Poster

Abstract Data Types is a way to generalize/model software so that the same interface used with different implementations and types.

Standard Template Library is a good example of Abstract Data Types. For example the interface for stack is independent of the implementation. Under the hood stack may be implemented by linked list, array or whatever is best suited for the problem.

In addition using templates same software model can be used with different data type; floats int, pointer to people objects etc. Below is an example of std::stack used with int and float.

#include <iostream>
#include <stack>


int main( int argc, char *argv[])
{
    // creat int stack
    std::stack<int> i;
    i.push(1);  i.push(3);  i.push(5);

    // create float stack
    std::stack<float> f;
    f.push(2.9884); f.push(4.8885); f.push(6.444);

    // show int stack
    while( !i.empty() )
    {
        std::cout << i.top() << std::endl;
        i.pop();
    }
    
    // show float stack
    while( !f.empty() )
    {
        std::cout << f.top() << std::endl;
        f.pop();
    }

    return 0;
}
template<> 37 Junior Poster

You are on the right track, below is sample of array using index and pointer.

#include <iostream>

int main( int argc, char *argv[])
{
    const size_t LIMIT = 8;
    int buffer[LIMIT] ={0};

    // array access using index notationn
    for(size_t i=0; i < LIMIT; i++)
    {
        buffer[i] = i;
    }
    
    // array access using pointer notation
    for(size_t j=0; j < LIMIT; j++)
    {
        std::cout << *(buffer+j) << std::endl;
    }

    return 0;
}
template<> 37 Junior Poster

As your sample (int *ptr = new int[0]) demonstrates; an array of zero returns a valid pointer, not null. This is intentional since it’s specifically documented in the C++ standard.

Contrast this to malloc, where the return value under this condition is implementation depended.

I agree with you, it seems strange to allocate an array of zero bytes, which you cannot reference. But consider that when you do access an array you need to range check anyway.

Most likely this behavior is provided for generic programming/template support, where passing a zero size array may be needed.

template<> 37 Junior Poster

A problem unique to multi-core is that each core has its own cache and caches between cores must be synchronized if different threads are accessing the same variable.

So you may get into a situation where a shared resource has different values in the different cache, i.e. the caches did not synchronize. For example this may happen if a shared resource is not declared volatile.

gerard4143 commented: Interesting point +5
template<> 37 Junior Poster

If you must use clock then you problably do'nt want to use difftime since difftime is expecting time_t as input rather than clock_t. Note the function signature. double difftime ( time_t time2, time_t time1 );

Sample code show what happens when you pass clock_t into difftime rather than using time_t. You get a much larger delay.

1 #include <iostream>
  2 #include <time.h>
  3 
  4 
  5 int main()
  6 {
  7     clock_t c1 = clock();
  8     time_t t1 = time(0);
  9     
 10     sleep(1); 
 11     
 12     time_t t2 = time(0);
 13     clock_t c2 = clock();
 14     
 15     size_t cdiff = difftime(c2,c1);
 16     size_t tdiff = difftime(t2,t1);
 17     
 18     std::cout << "clock diff=[" << cdiff << "]" << std::endl;
 19     std::cout << "time diff=[" << tdiff << "]" << std::endl;
 20     
 21     return 0;
 22 }
template<> 37 Junior Poster

Are you trying to define the implementation outside the class like below?

class SomeClass
{ 
public:
     void doSomething();
};

void SomeClass::doSomething()
{

}
template<> 37 Junior Poster

Do you know how to use classes yet?

It would be cleaner to model your problem using classes.

template<> 37 Junior Poster

1. Iterate through all items in the vector: for loop below
2. find item in vector that matches your item to remove: *i == who_
3. remove item from vector using iterator: _vector.erase(i)

for( TVec::iterator i=_vector.begin(); i!=_vector.end(); ++i )
        {
            if( *i == who_ )
            {
                std::cout << who_ << " is removed" << std::endl;
                _vector.erase(i); return;
            }
        }
template<> 37 Junior Poster

Comparing floating point numbers using == or != is not safe. The difference between the two numbers must be compared to some tolerance, usually the system epsilon. (see sample code below).

bool isEqual( float v1, float v2 )
{
    if( std::abs(v1-v2) < std::numeric_limits<float>::epsilon() )
    {
        return true;
    }
    
    return false;
}
ravenous commented: Helpful information +6
template<> 37 Junior Poster

Access first letter using index 0 not 1, but make sure user entered a middle name by checking size()

int main( int argc, char *argv[])
{
    std::string middle;
    std::cout<<"Enter your middle name: "; std::getline(std::cin,middle);

    if( middle.size() )
    {
        std::cout << "middle initial is=[" << middle[0]  << "]" << std::endl;
    }
    else
    {
        std::cout << "no middle name" << std::endl;
    }
}
template<> 37 Junior Poster
template<> 37 Junior Poster

Learn to think and model your problem in terms of objects and object interactions. The c++ language constructs lets you model your ideas into code. Search Google for "c++ object class design" for more information.

template<> 37 Junior Poster

According to 2003 C++ Specification this is legal. An array with no elements is created.

template<> 37 Junior Poster

Below is a example of one way to remove an item from a vector using an iterator.

class Eraser
{
public:
    Eraser()
    {
        _vector.push_back("fred");
        _vector.push_back("alice");
        _vector.push_back("billy");
    }
    bool remove(const std::string &who_)
    {
        for( TVec::iterator i=_vector.begin(); i!=_vector.end(); ++i )
        {
            if( *i == who_ )
            {
                std::cout << who_ << " is removed" << std::endl;
                _vector.erase(i);
                return true;
            }
        }

        std::cout << who_ << " is not in list" << std::endl;
        return false;
    }
    void show()
    {
        for( size_t i=0; i < _vector.size(); i++ )
        {
            std::cout << _vector[i] << std::endl;
        }
    }
private:
    typedef std::vector<std::string> TVec;
    TVec _vector;
};