Problem mit find
-
Wenn ich versuche einen key mit find zu suchen klappt es nicht aber begin und funktionieren.
#include <iostream> #include <string> #include <fstream> #include <vector> #include <map> #include "include/libhtml++.h" #include "cgi.h" #define CGI using std::vector; using namespace std; * The cgi request function get the query string from the cgi query */ bool request() { query_string = string(); char *request = getenv("REQUEST_METHOD"); char *env = getenv("QUERY_STRING"); char *clength = getenv ("CONTENT_LENGTH"); if( request == 0 ) { return false; } else if(request == std::string("GET")) { if (env != 0) { query_string = env; return true; } else return false; } else if(request == std::string("POST")) { if (clength != 0) { getline(cin, query_string);; return true; } else return false; } else return false; } /* This function formated the cgi input. */ void format() { Query query; unsigned int len; len = query_string.length(); size_t pos = 0; size_t old_pos = 0; while(query_string.find("+") != string::npos ) { query_string.replace(query_string.find("+") ,1, " "); }; while(len > pos) { pos = query_string.find("&", old_pos); if (pos == string::npos) pos = query_string.length(); string paar = query_string.substr(old_pos, pos-old_pos); size_t eq_pos = paar.find("="); string schluessel = paar.substr(0, eq_pos); string wert = paar.substr(eq_pos+1); query.insert(make_pair(schluessel, wert)); old_pos = pos+1; }; /* ausgabe funktoniert */ map<string,string>::iterator iter; for( iter = query.begin(); iter != query.end(); ++iter ) { cout << "Key: '" << iter->first << "', Value: " << iter->second << endl; } /* ausgabe klappt nicht obwohl key exitiert. */ map<string,string>::iterator iter = query.find("test"); t if( iter != query.end() ) { cout << "You typed '" << iter->first << "' " << iter->second << " time(s)" << endl; } }cgi.h
#include <iostream> #include <map> using namespace std; typedef std::map< string, string > Query; string query_string;
-
Geht das, dass man nem map mit string hat und nach char* sucht?
-
Es gibt nen string-Konstruktor der ein const cchar* entgegennimmt, der wird dannohl aufgerufen um ein temporäres Objekt zu erstellen, das an find übergeben wird. Oder kurz, ja.
-
Hab gerade keine Zeit alles aufzuräumen ... aber 1x:
#include <cstdlib> #include <cstring> bool request(std::string& dest) { char* request_method = std::getenv("REQUEST_METHOD"); if (request_method == NULL) return false; if (std::strcmp(request_method, "GET") == 0) { char* query = std::getenv("QUERY_STRING") if (query == NULL) return false; dest = query; } else if (std::strcmp(request_method, "POST") == 0) { char* content_length = std::getenv("CONTENT_LENGTH"); if (content_length == NULL) return false; std::getline(std::cin, dest); } return true; }... usw.

-
Tuxist schrieb:
/* ausgabe funktoniert */ map<string,string>::iterator iter; for( iter = query.begin(); iter != query.end(); ++iter ) { cout << "Key: '" << iter->first << "', Value: " << iter->second << endl; } /* ausgabe klappt nicht obwohl key exitiert. */ map<string,string>::iterator iter = query.find("test"); t if( iter != query.end() ) { cout << "You typed '" << iter->first << "' " << iter->second << " time(s)" << endl; } }cgi.h
#include <iostream> #include <map> using namespace std; typedef std::map< string, string > Query; string query_string;ich hoffe mal, dass das t hinter dem query.find() sowie das grossgeschriebene Query tippfehler sind. - Wie sieht denn der Output aus den diese Zeilen verursachen?
-
void format(std::string query_string, std::map<std::string, std::string>& dest) { for (std::size_t pos = query_string.find("+"); pos != std::string::npos; pos = query_string.find("+", ++pos)) query_string.replace(pos, 1, " "); const std::size_t query_string_length(query_string.length()); Query query; size_t pos(0); size_t old_pos(0); while (len > pos) { pos = query_string.find("&", old_pos); if (pos == std::string::npos) pos = query_string_len; const std::string data(query_string.substr(old_pos, pos - old_pos)); old_pos = ++pos; const std::size_t value_pos(data.find("=")); query.insert(std:::make_pair(data.substr(0, value_pos), data.substr(++value_pos))); } }das query_string eine Kopie ist absicht, da der String ja modifiziert wird.
-
wenn ich das query klein schreib geht es nicht da das große einen map typ definiert und das kleine eine std::map ist oder habe ich etwas falsch verstanden ?