|
To add to the other explanations - what makes vector cool is that it shares a common interface with other container classes through a thing called an iterator. While you can use array notation to access a vectors members, you can use iterators to step through them also, and the STL contains a lot of algorithms that take iterators and work on containers to organise and process their contents.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Whooop! Looks like they forgot to preview.
Variable array of intergers:
std::vector<int> int_array;
Most used methods:
back(): returns a reference to the last element.
pop_back(): remove the last element.
operator[]: returns a refernce a specific element.
push_back(element): adds an element to the end of array.
begin(): returns iterator of first element.
end(): returns interactor just passed the last element.
capacity(): the number of elements it can hold before it has to reallocate storage.
size(): the number of elements currently stored.
erase(...): removes 1 or more elements from array.
clear(): removes all elements from array.
empty(): (size() < 1)
reserve(n): reserves extra storage space (capacity); if you know approximately how many elements you need to store, then reserving that amount will reduce the number of reallocations required (thus increasing the speed of insertion and reducing memory fragmentaion).
typedef std::vector<int> int_array_type;
int_array_type int_array;
for( int i=0; i<int_array.size(); ++i )
{
if( some_int_value == int_array[i] )
}
int_array_type::const_iterator i = int_array.begin();
for( ; i != int_array.end(); ++i )
{
if( some_int_value == *i )
}
Hope that helps.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
I have a CList containing objects. I want to know if I can use a vector on this CLIst or do I have to use vector<myojb> instead of CList.
The issue here is that I need a container that I can easily add/remove objects to, test to see if its empty and sometimes apply a function to that object.
This is my first time thinking of using vectors, but I like the for_each option instead of using a 'typical' for loop using CList.GetNext(pos).
ANy ideas?
|
|
|
|
|
vectors and the rest of the standard C++ containers rock. i haven't used an MFC container in new code in years.
you might also look at std::list, if you really need a list and not an array - subtle difference that might not be important for your use.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
You're comparing apples and oranges for two reasons
1/ MFC containers are crap, you should be using the STL
2/ A CList is a list of linked nodes, faster for random insertions, slower for random access. You should look at the list class, unless you have a reason to go to an array ( fast for random lookup, slow for random insertions ). Then vector is what you want.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi,
I am kind of new to multi threading.I am doing a project in vc++ win32.
I am using two threads - one for reading from port and another for writing to the port.
But when I execute...sometimes the flow gets mixed up..I want the read thread to go first and when it is done I want to start the write thread and then again read thread...I dont want start one thread while other is going on..
How do I accompilish this..I want have a state m/c kind of..but have no idea how to do one..
pls help...thanks in advance..
|
|
|
|
|
You have two threads. Can't each one wait on an Event set by the other?
The read thread waits to do work until the write thread sets an event. The write thread waits for the Read thread to set an event. Each one sets the even the other is waiting for at the end of its own processing, signaling it is safe for the other to take over. I would think they would flop back and forth as you desire. It should not deadlock, I don't think, because neither one is waiting on both of the events to be signaled. You could always put a timeout in one of the threads that should have higher priority, in the case they are both idle waiting for 'the other guy' to set the event.
It would also be ideal if the port could set an event or some signel to let you know data was available for reading. I would think your process is the one repsonsible for any writing that occurs.
|
|
|
|
|
Ann66 wrote:
I dont want start one thread while other is going on
This leads me to believe you don't need a multi-threaded design. A single thread that sequentially reads and writes should work.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
UNLESS the application is doing something ELSE while it is sitting there waiting for a read or write. I suspect they need at least two threads. The program's primary thread and a single secondary thread for the IO. It seems they read, and want to process data and then write, or something like that. I tend to agree that maybe a separate thread is not required to separate the reading from the writing, but I would put the IO in a separate thread so it can wait to receive data, or else not tie up rest of application performing synchronous writes or whatever it is they are doing.
|
|
|
|
|
Blake Miller wrote:
I tend to agree that maybe a separate thread is not required to separate the reading from the writing, but I would put the IO in a separate thread...
Yes, that's what I meant.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Hey there,
I have an application which lists the following menu options "File" " View" "Help" etc.
And ofcourse within each of these is a further list of options. I want to write an application which will automatically nagivate through this menu ... As an example...suppose I want to go to View->Text Size -> Normal, I can do so through my application at the click of a button.
Is anyone aware of the kind of messages I could send or provide an example of any such code you may have.
Regards,
Abhishek
|
|
|
|
|
If i have a struct defined as :
typedef struct _mystruct
{
TCHAR Name;
TCHAR Address;
}Info
And i would like to populate this struct , what is wrong with me doing
Info* pInfo;
pInfo->Name = "Mark";
pInfo->Address = "Mark's Address";
I can use a StrCpy(pInfo->Name, "Mark"); in an MFC environment
but this is in a win32 dll and no MFC stuff available
Can i get some help on how i can do this?
|
|
|
|
|
First of all your 'Name' and 'Address' fields in
the struct are only one TCHAR long each, so you
can't assign string values to them using any
method.
You must either declare them as arrays of TCHAR,
large enough to hold the longest expected string
value or declare them as pointers and allocate
memory for each string as required.
You can then use strcpy() (#include <string.h>).
This is not an MFC function.
You can only use the '=' operator on strings when
using CString, which is an MFC.
Steve T
|
|
|
|
|
ok you got quite some errors there..
1) TCHAR represents a single character (wide char if UNICODE is defined ANSI char if not). So if you wish to have the members name and Address to contain strings they should be arrays!
e.g. TCHAR Name[50];
2) your pInfo variable should point to a legal location... in the example you posted it points to ???
Info* pInfo = new Info;
or
Info anInfoStr;<br />
Info* pInfo = &anInfoStr;<br />
3) a TCHAR string is not a class and has no defualt assignment operator.. presuming you made your Name and Adress members arrays you should copy the strings.
_tcscpy(pInfo->Name, _T("Mark"));<br />
_tcscpy(pInfo->Address, _T("Mark's Address"));
Hope this clears things up a bit...
Greetings,
Davy
|
|
|
|
|
If you're programming in C++, you can use the C++ templated string classes.
typedef struct _mystruct
{
typedef std::basic_string<TCHAR> mystring_type;
mystring_type Name;
mystring_type Addres;
} MyInfo;
pInfo->Name = _T("Mark");
pInfo->Address = _T("Marks's Address");
Of cource saving the data to a file is a totaly different subject; you cann't do anything like write(hFile,pInfo,sizeof(MyInfo)), because it would not make since with this structure (or any structure that contains pointers).
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
for Windows Task Manager, We click Applications to see Running tasks, But
can I write code to see them?
|
|
|
|
|
The EnumProcesses function retrieves the process identifier for each process object in the system.
Use OpenProcess to get process handle.
Use functions like GetModuleFileNameEx , GetProcessMemoryInfo to obtain some information.
Don't forget to call CloseHandle() on the process handle when done.
|
|
|
|
|
|
This is my 1st time in a Forum, so here it goes...
I have something like this
void CChildView::sortData(LPCTSTR sendData)
{
//I want to insert sendData into an array.
//the compiler, visual C++ 6 will not allow me to.
//sendData is only 1 character at a time send from the COM-port.
//I've tried:
strcpy (tempData, sendData);
//and many more to mention
//Also after editing, I want to send it back to:
DispalyData(tempData);
//DisplayData will not accept anything else but the LPCTSTR format.
}
I've been working on this for 2 whole days, 18hrs!!! Could you please help me, I am a beginer in programming, and have read all the reading materials on the net, but don't understand as it's to advance for me.
Thank you.
All the evidence seems to indicate, that the universe has not existed forever, but that it had a beginning, about 15 billion years ago. This is probably the most remarkable discovery of modern cosmology. Yet it is now taken for granted. We are not yet certain whether the universe will have an end. When I gave a lecture in Japan, I was asked not to mention the possible re-collapse of the universe, because it might affect the stock market.
-- Stephen Hawking
|
|
|
|
|
Long
Pointer
Constant
Translates to either Unicode or ANSI
STRing
LPCTSTR lpName = "tomek18";
char cLetter = lpName[3]; or
char cLetter = *(lpName + 3); tomek18 wrote:
//I want to insert sendData into an array.
So what does the array look like?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
void CChildView::sortData(LPCTSTR sendData)
{
TCHAR buffer[1024];
_sntprintf(buffer, 1024, _T("%s"), sendData);
DisplayData(buffer);
}
|
|
|
|
|
typedef wchar_t TCHAR; // UNICODE
typedef char TCHAR; //ANSI, DBCS
typedef const TCHAR* LPCTSTR;
Therefor, if you are compiling a UNICODE program then LPCTSTR is defined as const wchar_t*, otherwise it is defined as const char*.
If want to be able to create ANSI and UNICODE version of your code, then you need to use TCHARs and the macro _T()(or TEXT()), along with the functions define to use TCHARs (normaly they start with _tc???()).
If all you need to work with is ANSI, then you can use plain old C-strings: char, char*, const char*.
size of in BYTEs = (string length in TCHARs)/sizeof(TCHAR).
byte_len = _tcslen(str)/sizeof(TCHAR);
byte_len = strlen(str)/sizeof(char); // equal to strlen()/1
byte_len = wcslen(str)/sizeof(wchar_t); // wcslen(str)/2
Good Luck!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Does anyone know any common/basic time functions in C / C++?
I want to write a frequency function that will call another function, every set period. For example, if the period was set for every 5 seconds, then the function will 'tic' every 5 seconds.
I notice there is no built-in functions for this sort of clock / tic functionaility. What is the easiest way? Or the best most efficient way or implementing a clock / tic function?
C method first, then if it's better, C++.
|
|
|
|
|
|
I was looking for a non-Visual C++ specific way. Someway by the standard C library. I believe SetTimer() is a Microsoft C++ function.
|
|
|
|