Difference between revisions of "Assignment 3i: Q & A"

From CDOT Wiki
Jump to: navigation, search
(Created page with ''''Q: Can you remove an element from the middle of of the vector container?'''')
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''Q: Can you remove an element from the middle of of the vector container?'''
+
'''Q: Can you remove an element from the middle of the vector container?''' submitted by Imtiaz Latif & Team(2&10) <br>
 +
'''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: [http://www.cplusplus.com/reference/stl/vector/erase/ Vector erase()]
 +
 
 +
(Submitted by Kevin Kofler, Team 9)<br>
 +
 
 +
<br>
 +
'''Q:''' How to get current system time using c++?<br>
 +
'''A:''' There is time_t data type in c++ which when passed as an argument to time function represents calendar time.<br>
 +
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.<br>
 +
<source lang=html4strict>
 +
#include <ctime>
 +
#include <cstdio>
 +
using namespace std;
 +
int main()
 +
{
 +
time_t ltime;
 +
time(&ltime);
 +
printf("Size of time_t is %d\n",sizeof(time_t));
 +
printf("Time is %s\n",ctime(&ltime));
 +
return 0;
 +
}
 +
</source>
 +
'''Submitted by:''' Shajinth Pathmakulaseelan, Team 2 <br><br>
 +
 
 +
'''Q:''' What is the easiest way to delete all the contents of file? <br>
 +
'''A:''' The easiest way to delete all contents of file is to open file in write mode.<br>
 +
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.<br>
 +
<source lang=html4strict>
 +
        FILE *fp = fopen("filename","w");
 +
</source>
 +
'''Submitted by:''' Shajinth Pathmakulaseelan, Team 2 <br><br>

Latest revision as of 18:56, 5 December 2012

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;
	time(&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