Assignment 3i: Q & A

From CDOT Wiki
Revision as of 18:56, 5 December 2012 by Ilatif (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Q: Can you remove an element from the middle of the vector container? submitted by Imtiaz Latif & Team(2&10)
A: Yes, by using the Standard Library vector member function erase(), which has two implementations: one, iterator erase(iterator postion) will erase the element at the passed iterator position, while the other, iterator erase(iterator first, iterator last) will erase ALL the elements in the specified range.

More info here: Vector erase()

(Submitted by Kevin Kofler, Team 9)

Q: How to get current system time using c++?
A: There is time_t data type in c++ which when passed as an argument to time function represents calendar time.
Its actual value is number of seconds elapsed since 00:00:00 on January 1, 1970. Following code snippet will print your current system time.

	#include <ctime>
	#include <cstdio>
	using namespace std;
	int main()
	time_t ltime;
	printf("Size of time_t is %d\n",sizeof(time_t));
	printf("Time is %s\n",ctime(&ltime));
	return 0;

Submitted by: Shajinth Pathmakulaseelan, Team 2

Q: What is the easiest way to delete all the contents of file?
A: The easiest way to delete all contents of file is to open file in write mode.
When we open file in write mode then if file exists all its previous contents are erased and writing will start from the very beginning.

        FILE *fp = fopen("filename","w");

Submitted by: Shajinth Pathmakulaseelan, Team 2