| | |
string to vector<string> and splitting a string. Need help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 48
Reputation:
Solved Threads: 0
Hello, I'm getting the following error with this code, also i would like to know how to add the path and filespec together with a and be able to split the 2 so it would be 3 args not 4.
ERROR:
error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'char *' to 'const std::allocator<_Ty> &'
This is my current code:
ERROR:
error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'char *' to 'const std::allocator<_Ty> &'
This is my current code:
C++ Syntax (Toggle Plain Text)
#include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> using namespace boost::filesystem; #include <boost/regex.hpp> void show_files( const path & directory, bool recurse_into_subdirs = false ) { if( exists( directory ) ) { directory_iterator end ; for( directory_iterator iter(directory) ; iter != end ; ++iter ) if ( is_directory( *iter ) ) { cout << "\nd " << iter->path() ; if( recurse_into_subdirs ) show_files(*iter) ; } else cout << "\n- " << iter->path() ; } } int main(int argc, char *argv[]) { if (argc != 4) { cout << "Usage: program.exe [switches] [path] [filespec]" << endl; } else { vector<string> s_switches(argv[1]); string s_path(argv[2]); string s_filespec(argv[3]); cout << s_switches[1] << endl; bool aSwitch = false; bool rSwitch = false; bool RSwitch = false; bool lSwitch = false; //set switches if (s_switches[1] == "-a") { bool aSwitch = true; } if (s_switches[1] == "-r") { bool rSwitch = true; } if (s_switches[1] == "-R") { bool RSwitch = true; } if (s_switches[1] == "-l") { bool lSwitch = true; } //boost::regex expr(s_filespec); //fs::path currentPath(s_path); //cout << currentPath << endl; //fs::basic_directory_iterator(s_path); show_files(s_path) ; } cin.ignore(); return 0; }
Last edited by Argo54325; Mar 31st, 2009 at 8:37 pm.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
Instead of:
try:
cpp Syntax (Toggle Plain Text)
vector<string> s_switches(argv[1]);
cpp Syntax (Toggle Plain Text)
vector<string> s_switches(string(argv[1]));
Last edited by adam1122; Mar 31st, 2009 at 8:53 pm.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
That line isn't going to work as you want it to. It's not going to pull in all of the "switch" arguments.
You're using Boost already. Why not use program options (http://www.boost.org/doc/libs/1_38_0...m_options.html) from Boost?
You're using Boost already. Why not use program options (http://www.boost.org/doc/libs/1_38_0...m_options.html) from Boost?
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
I would encourage you to look into it for the future but I can understand you probably just want to get this done on schedule.
One solution would be to not assume that there are 4 arguments, because you have 4 optional switches and 2 other arguments, in addition to the program name.
Since the last two arguments are mandatory, you access them as the last argument in the
One solution would be to not assume that there are 4 arguments, because you have 4 optional switches and 2 other arguments, in addition to the program name.
Since the last two arguments are mandatory, you access them as the last argument in the
argv and the second to last argument in argv . The switch arguments are then everything from argv[1] to argv[argc-3] •
•
Join Date: Feb 2009
Posts: 48
Reputation:
Solved Threads: 0
SO you saying to have more then 4 args? my prof only wants 3, the switches should be -r-R-l for example and the next arg should be C:\program files (x86)\.... (then a character that would separate the 2, i chose |) followed by the regular expression, eg. (B|b)ill.*
All i need to know for now is how to separate them, once i can get that done i can work on using what has been specified.
All i need to know for now is how to separate them, once i can get that done i can work on using what has been specified.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
I see, they made it a little easier for you.
Just treat the switches as a string like you do with the other arguments. Forget the vector.
You then just parse the string of switches. One solution would be to use a for loop and the
Your loop could look like this:
I'll leave the body of the loop for you since I probably shouldn't do your hw. But what you would want to do is access the element at x and x+1 to get a single switch (e.g. -r). You can get parts of the string with substr. A reference is here: http://www.cplusplus.com/reference/string/string/
Also note that if the size of the switch isn't divisible by 2, it's not valid. You might want to check for this before the loop.
Just treat the switches as a string like you do with the other arguments. Forget the vector.
You then just parse the string of switches. One solution would be to use a for loop and the
substr method of string .Your loop could look like this:
C++ Syntax (Toggle Plain Text)
for(int x=0; x < s_switches.size(); x+=2)
I'll leave the body of the loop for you since I probably shouldn't do your hw. But what you would want to do is access the element at x and x+1 to get a single switch (e.g. -r). You can get parts of the string with substr. A reference is here: http://www.cplusplus.com/reference/string/string/
Also note that if the size of the switch isn't divisible by 2, it's not valid. You might want to check for this before the loop.
![]() |
Similar Threads
- splitting sentance into words (C++)
- Debug Assertion Failed! (C++)
- Strings from file (C++)
- Reading through a vector (C++)
- Splitting a String using a delimiter (C++)
- Parse or Tokenize String (C++)
- Splitting words from string into character array (C++)
- PHP 'explode()' in C++ ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Installing a Jinitiator
- Next Thread: linked list
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





