English 中文(简体)
Comparing character arrays and string literals in C++ without cstring
原标题:

In my programming class we currently have a project that requires us to take arguments into the program. I then need to be able to check one of the arguments to see which value was passed to the program so that I can choose the appropriate behavior for the program to follow. In a previous homework assignment I did this with the strcmp function included in the cstring library using the following code:

int main(int argc, char *argv[]) {
   if (strcmp(argv[1], "yes") == 0) {
      // do this code
   } else if (strcmp(argv[1], "no") == 0) {
      // do this code
   }
}

However, for some reason we re not allowed to use the cstring library in this project. How else can I do this?

最佳回答

Try this:

if (argv[1] == std::string("yes")) { stuff }

If the intent of the exercise is to teach how string comparisons work, then implement a for loop like other answers suggest. However, in C++ you are not supposed to use strcmp - there is a string class for a reason.

问题回答

Make your own my_string_comp function:

// return 0 on success, -1 on failure
int my_string_comp(char* a, char *b) {
  if(a && b) { // check for non null pointers
    while(*a !=    && *b !=    && *a == *b) { a++; b++; }
    if(*a ==    && *b ==   ) { return 0; }
  }  
  return -1;
}

Note that this function gives a boolean output whereas strcmp returns values corresponding to lexicographical order of two input strings.

write your own strcmp function

You can always compare strings using a manual for loop, comparing it character by character. Your terminating condition will be when you encounter a NULL-char (since in C strings are null-terminated) or when you encounter a character in the first string which is not equal to its counterpart in the second string.

const char* s1 = "abcdefg";
const char* s2 = "abcdefg";

const char* p1 = s1;
const char* p2 = s2;

int same = 0;
for (; (same = (*p1 == *p2)) && *p1 !=   ; ++p1, ++p2);

if (same) printf("The strings are equal
");
else printf("The strings are NOT equal
");

This will give you a character-by-character comparison. But note that this is different from strcmp in the sense that strcmp does a lexicographical comparison which will also tell you if one string is "less" than other string in terms of alphabetical ordering. But it seems you only need an equality comparison, which will only tell you whether or not the strings are identical.

You can hash the input string and switch based on its numerical value - for the case values use the hashed values of the symbolic constants. You could make an external script to generate a header file with the "string" to "hash" mapping.

I highly, highly recommend Boost.Program options for C++ command-line argument processing. It may be overkill for something like this, but for anything even slightly more complicated, the Boost.Program options library is less code, easier to understand once written, and lets you do fancy things like taking arguments from config files or from environment variables, or passing on the command line, with almost no work.

Something roughly like the following (untested) should work for you:

namespace po = boost::program_options;

int main() {
  po::options_description desc("Allowed options");
  desc.add_options()
    ("help", "produce help message")
    ("yes",  "get down")
    ("no",   "get funky")
  ;

  po:variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);

  if (vm.count("yes")) {
    // get down
  } else if (vm.count("no")) {
    // get funky
  }
}

The tutorial page gives a number of other examples which shows that it is almost no work to add additional, substantially more complex, command-line arguments.

Note that boost is not included by default in C++, but it is not normally too hard to add it. Boost is one of those sets of libraries that all C++ programmers need to be aware of. Please be warned that this may be overkill if you are still learning C++, however.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签