Hello again. Thanks to all that contributed positively to my first thread. it went a long way.
Now, i am here again. can someone please help me out in the case of vectors.i.e. [#include<vector>]? Please do not be angry at me for posting such oversimple questions,i am a dumb beginner who is trying to catch on. can someone help me out with a c++ beginners'
book?
Thanks for your cooperation.

Dani AI

Generated

asked for a simple introduction to std::vector and a beginner book. suggested a web search; posted the std_lib_facilities.h header (the helper header used in Stroustrup's course), and warned that dropping that header on a newcomer is overwhelming. The short, practical guidance below explains what std::vector is, gives a minimal example, lists common pitfalls, and recommends next steps.

A std::vector is a dynamic, contiguous array that grows automatically. Common operations are push_back, pop_back, size, capacity, indexing with operator[], and bounds-checked access with at(). A minimal example (modern and compatible idioms shown) follows.

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;
    v.push_back(10);
    v.push_back(20);

    for (std::size_t i = 0; i < v.size(); ++i)
        std::cout << v[i] << '\n';

    // C++11 range-based loop:
    for (int x : v)
        std::cout << x << '\n';
}

Practical tips and common pitfalls:

  • operator[] does not check bounds; at() throws std::out_of_range on bad index.
  • Reallocation invalidates pointers, references and iterators. Use reserve() when size growth is known to avoid surprises.
  • size() returns an unsigned size_type; avoid signed/unsigned comparison bugs when iterating.
  • Prefer emplace_back() (C++11+) to construct elements in place, and avoid std::vector<bool> unless its bit-specialization is acceptable.
  • The header shown by provides extra checks for learning, but redefining or macro-wrapping vector can hide standard behavior and is best used only after the basics are understood.

Good next resources: practical exercises plus the reference pages at cppreference vector reference and cplusplus.com vector tutorial. For books, many learners start with "Accelerated C++" for hands-on practice and move to Stroustrup's course book for a deeper foundation.

Recommended Answers

All 4 Replies

<~~ another noob.

#include <vector>

save this in your include file folder as std_lib_facilities.h. don't forget to include in your code?

/*
	simple "Programming: Principles and Practice using C++" course header to
	be used for the first few weeks.
	It provides the most common standard headers (in the global namespace)
	and minimal exception/error support.

	Students: please don't try to understand the details of headers just yet.
	All will be explained. This header is primarily used so that you don't have
	to understand every concept all at once.
*/

#ifndef H112
#define H112 200608L

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
#include<stdexcept>
using namespace std;


template<class T> string to_string(const T& t)
{
	ostringstream os;
	os << t;
	return os.str();
}

struct Range_error : out_of_range {	// enhanced vector range error reporting
	int index;
	Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
	typedef typename std::vector<T>::size_type size_type;

	Vector() { }
	explicit Vector(size_type n) :std::vector<T>(n) {}
	Vector(size_type n, const T& v) :std::vector<T>(n,v) {}

	T& operator[](unsigned int i) // rather than return at(i);
	{
		if (i<0||this->size()<=i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
	const T& operator[](unsigned int i) const
	{
		if (i<0||this->size()<=i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
	
	String() { }
	String(const char* p) :std::string(p) {}
	String(const string& s) :std::string(s) {}
	String(int sz, char val) :std::string(sz,val) {}
	template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }

	char& operator[](unsigned int i) // rather than return at(i);
	{
		if (i<0||size()<=i) throw Range_error(i);
		return std::string::operator[](i);
	}

	const char& operator[](unsigned int i) const
	{
		if (i<0||size()<=i) throw Range_error(i);
		return std::string::operator[](i);
	}
};



struct Exit : runtime_error {
	Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
	throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
	error(s+s2);
}

inline void error(const string& s, int i)
{
	ostringstream os;
	os << s <<": " << i;
	error(os.str());
}

#if _MSC_VER<1500
	// disgusting macro hack to get a range checked string:
	#define string String
	// MS C++ 9.0 have a built-in assert for string range check
	// and uses "std::string" in several places so that macro substitution fails
#endif

template<class T> char* as_bytes(T& i)	// needed for binary I/O
{
	void* addr = &i;	// get the address of the first byte
						// of memory used to store the object
	return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
	cin.clear();
	cout << "Please enter a character to exit\n";
	char ch;
	cin >> ch;
	return;
}

inline void keep_window_open(string s)
{
	if (s=="") return;
	cin.clear();
	cin.ignore(120,'\n');
	for (;;) {
		cout << "Please enter " << s << " to exit\n";
		string ss;
		while (cin >> ss && ss!=s)
			cout << "Please enter " << s << " to exit\n";
		return;
	}
}

// make std::min() and std::max() accessible:
#undef min
#undef max

#include<iomanip>
inline ios_base& general(ios_base& b)	// to augment fixed and scientific
{
	b.setf(ios_base::fmtflags(0),ios_base::floatfield);
	return b;
}

// run-time checked narrowing cast (type conversion):
template<class R, class A> R narrow_cast(const A& a)
{
	R r = a;
	if (A(r)!=a) error(string("info loss"));
	return r;
}


inline int randint(int max) { return rand()%max; }

inline int randint(int min, int max) { return randint(max-min)+min; }

#endif

He asked for a book to learn, not for 170+ lines of code that he will have no idea how to use. He is trying to learn. Your code does absolutely nothing to help a beginner understand how vectors actually work.
@OP follow Ancient Dragon's link for help with vectors. :)

:))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.