STL iterators?
-
Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.
typedef vector TFileList; typedef TFileList::iterator TFileListIt; CStringList strlistFiles; fsDlg.SelectFile(&strlistFiles); int nFiles = strlistFiles.GetCount(); if (nFiles > 0) { int nIndex; TFileList FileList(nFiles); CString strFileName, strNewFileName; POSITION pos; // Populate the list with the pointers to the file names. for (nIndex = 0; nIndex < nFiles; nIndex++) { pos = strlistFiles.FindIndex(nIndex); FileList.push\_back(&strlistFiles.GetAt(pos)); } TFileListIt start, end, it; start = FileList.begin(); end = FileList.end(); for (it = start; it != end; it++) { TRACE("> %s\\n", \*\*it); // <-- Access violation! } }
Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas." -
Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.
typedef vector TFileList; typedef TFileList::iterator TFileListIt; CStringList strlistFiles; fsDlg.SelectFile(&strlistFiles); int nFiles = strlistFiles.GetCount(); if (nFiles > 0) { int nIndex; TFileList FileList(nFiles); CString strFileName, strNewFileName; POSITION pos; // Populate the list with the pointers to the file names. for (nIndex = 0; nIndex < nFiles; nIndex++) { pos = strlistFiles.FindIndex(nIndex); FileList.push\_back(&strlistFiles.GetAt(pos)); } TFileListIt start, end, it; start = FileList.begin(); end = FileList.end(); for (it = start; it != end; it++) { TRACE("> %s\\n", \*\*it); // <-- Access violation! } }
Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas."Try *(*it)? (Just guessing :)). Best regards, Alexandru Savescu
-
Try *(*it)? (Just guessing :)). Best regards, Alexandru Savescu
The problem is that my vector seems to be empty, even though I am filling it with valid pointers.:( Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas." -
The problem is that my vector seems to be empty, even though I am filling it with valid pointers.:( Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas."Did you call size() to see if it is empty? Best regards, Alexandru Savescu
-
Did you call size() to see if it is empty? Best regards, Alexandru Savescu
size() returns 8, but I have added 4 pointers to the vector.:confused: I have noticed that the vector elements does not point to m_pchData of the string list elements I am using, but to some other address. _First and _Last of the vector are updated when adding to the vector. Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas." -
size() returns 8, but I have added 4 pointers to the vector.:confused: I have noticed that the vector elements does not point to m_pchData of the string list elements I am using, but to some other address. _First and _Last of the vector are updated when adding to the vector. Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas."Try not to store pointers, store CStrings directly. There is not much of a performance differnce since the CString does not get copied, its reference is increased. Best regards, Alexandru Savescu
-
Try not to store pointers, store CStrings directly. There is not much of a performance differnce since the CString does not get copied, its reference is increased. Best regards, Alexandru Savescu
Nah, didn't work either. The push_back seems broken... Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas." -
Why do I get an access violation when I try to get the value of the iterator "it" in this piece of code? When I look at the iterators they have no valid pointers to the strings.
typedef vector TFileList; typedef TFileList::iterator TFileListIt; CStringList strlistFiles; fsDlg.SelectFile(&strlistFiles); int nFiles = strlistFiles.GetCount(); if (nFiles > 0) { int nIndex; TFileList FileList(nFiles); CString strFileName, strNewFileName; POSITION pos; // Populate the list with the pointers to the file names. for (nIndex = 0; nIndex < nFiles; nIndex++) { pos = strlistFiles.FindIndex(nIndex); FileList.push\_back(&strlistFiles.GetAt(pos)); } TFileListIt start, end, it; start = FileList.begin(); end = FileList.end(); for (it = start; it != end; it++) { TRACE("> %s\\n", \*\*it); // <-- Access violation! } }
Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas."It's this line: Fredrik Skoog wrote: TFileList FileList(nFiles); This creates nFiles items initially empty and I guess NULL pointers. That's why you had 8 items, because 4 have already been created. You shoud use:
TFileList FileList;
FileList.reserve (nFiles);reserve allocates enough space, but does not create any items. Best regards, Alexandru Savescu
-
It's this line: Fredrik Skoog wrote: TFileList FileList(nFiles); This creates nFiles items initially empty and I guess NULL pointers. That's why you had 8 items, because 4 have already been created. You shoud use:
TFileList FileList;
FileList.reserve (nFiles);reserve allocates enough space, but does not create any items. Best regards, Alexandru Savescu
Yep, that's it. Thanks a million! I think I'll have to hit the books now :-O Cheers, Fredrik
Sonork ID: 100.11430:PhatBoy
"Felix qui potuit rerum cognoscere causas."