- need help with std::map - 9 Updates
- write a C++ program to print number of zeros of a series of 10 numbers entered by the user. - 4 Updates
- Output Stream Formatting - 2 Updates
- Onwards and upwards - 1 Update
- Upload file using wininet in c++. - 2 Updates
- What do you think about my new mock library? - 1 Update
- won't compile? - 2 Updates
- won't compile? - 1 Update
- won't compile? - 1 Update
- won't compile? - 1 Update
SpreadTooThin <bjobrien62@gmail.com>: Apr 02 08:32PM -0700 This is my map. std::map<string, std::vector<string> > myMap; Given that key is a string and the element that it represents is a vector of strings: How do I add an element to myMap? std::string key = "abc"; std::vector<string> * empty = new std::vector<string>; map[key]=*empty; To add an element to the vector withing the map std::vector<string> *myvect; myvect = map[key]; *myvect->push_back(newstring); Shouldn't I be using a refrence to an object rather than a pointer? |
Ian Collins <ian-news@hotmail.com>: Apr 03 04:41PM +1300 SpreadTooThin wrote: > myvect = map[key]; > *myvect->push_back(newstring); > Shouldn't I be using a refrence to an object rather than a pointer? Just add the vector: std::vector<string> empty; map[key]=empty; or if you just want a default initialised vector, map[key]; -- Ian Collins |
SpreadTooThin <bjobrien62@gmail.com>: Apr 02 08:51PM -0700 On Thursday, April 2, 2015 at 9:41:57 PM UTC-6, Ian Collins wrote: > map[key]; > -- > Ian Collins So when the line "std::vector<string> empty;" goes out of scope there is still a reference to it in the map and I can still use it? |
Ian Collins <ian-news@hotmail.com>: Apr 03 06:10PM +1300 SpreadTooThin wrote: >> or if you just want a default initialised vector, >> map[key]; > So when the line "std::vector<string> empty;" goes out of scope there is still a reference to it in the map and I can still use it? It's copied. C++ isn't Java! -- Ian Collins |
SpreadTooThin <bjobrien62@gmail.com>: Apr 03 10:10AM -0700 On Thursday, April 2, 2015 at 9:33:28 PM UTC-6, SpreadTooThin wrote: > myvect = map[key]; > *myvect->push_back(newstring); > Shouldn't I be using a refrence to an object rather than a pointer? std::vector<string> & key Isn't this a reference... Doesn't it seem inefficient to be copying objects... Like I'd rather use a pointer to an object but then I'd have to be careful about freeing it... |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Apr 03 05:33PM On Fri, 2015-04-03, SpreadTooThin wrote: >> Shouldn't I be using a refrence to an object rather than a pointer? > std::vector<string> & key > Isn't this a reference... Yes. > Doesn't it seem inefficient to be copying objects... > Like I'd rather use a pointer to an object but then > I'd have to be careful about freeing it... All the standard library containers copy. They own the objects they contain. That's the way it is. In the rare cases where this is inefficient, you'd store pointers or smart pointers instead -- but I don't think I ever had to, for that particular reason. (And this design got even cheaper with C++11.) Also see Ian Collins' posting. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
"Öö Tiib" <ootiib@hot.ee>: Apr 03 10:38AM -0700 On Friday, 3 April 2015 20:11:15 UTC+3, SpreadTooThin wrote: > std::vector<string> & key > Isn't this a reference... Doesn't it seem inefficient to be copying objects... > Like I'd rather use a pointer to an object but then I'd have to be careful about freeing it... All 'std::map', 'std::vector' and 'std::string' are dynamic containers. These can be copied or moved. Typically people copy since it is simpler and does not make much difference in performance: myMap.insert( std::make_pair( key, value ) ); // 'key' and 'value' are unchanged just copied from If too lot of copying will go on and it does make difference them people can change it: myMap.insert(std::make_pair( std::move(key), std::move(value)); // 'key' and 'value' are in valid but unspecified state after move |
Luca Risolia <luca.risolia@linux-projects.org>: Apr 03 08:55PM +0200 Il 03/04/2015 19:38, Öö Tiib ha scritto: > can change it: > myMap.insert(std::make_pair( std::move(key), std::move(value)); > // 'key' and 'value' are in valid but unspecified state after move In principle, using an emplacement function forwarding its arguments to the pair's constructor is a better alternative, as it can be more efficient and should never be less efficient than insertion. In particular, the proposed C++17's try_emplace() should definitely be a better alternative than insertion for the case of "possible rejected duplicates". |
"Öö Tiib" <ootiib@hot.ee>: Apr 03 04:12PM -0700 On Friday, 3 April 2015 21:55:58 UTC+3, Luca Risolia wrote: > particular, the proposed C++17's try_emplace() should definitely be a > better alternative than insertion for the case of "possible rejected > duplicates". It does look indeed shorter and more elegant. myMap.emplace( std::move(key), std::move(value) ); I can't measure that it performs any better. Perhaps new 'std::make_pair' and 'std::map::insert' are combining perfect forwarding cleverly enough that result is about same. Also it might be worth to note that on cases when the inserting code is fully certain that it is not attempt to insert a duplicate it usually also has some hint available for 'insert' overload that takes hint or 'emplace_hint'. Those perform slightly better than without hint. |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 03 02:30AM >{ unsigned long long count = 0; > for( int i = 0; i != 10; ++i )count += zerocount(); > ::std::cout << count << '\n'; } Next version has overflow detection, so it is not possible anymore to fool it by entering more than ULLONG_MAX zeros! #include <iostream> #include <ostream> #include <istream> #include <string> #include <algorithm> static inline auto zerocount() { ::std::string number; ::std::cin >> number; return count( number.begin(), number.end(), '0' ); } int main() { unsigned long long sum = 0; for( int i = 0; i != 10; ++i ) { unsigned long long count = zerocount(); if( ULLONG_MAX - count < sum )throw 0; sum += count; } ::std::cout << sum << '\n'; } |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 03 05:41PM >std::vector<string> & key >Isn't this a reference... It looks like an incomplete declaration. There is a declaration specifier std::vector<string> and a declarator & key , but no semicolon. Therefore, we cannot know whether it is complete yet. >Doesn't it seem inefficient to be copying objects... A C++ compiler will often implement a copy operation in a very efficient way, similar to a pointer copy. This is called a »move« and is being described in C++ textbooks. >Like I'd rather use a pointer to an object but then I'd have >to be careful about freeing it... You only need to free pointers that have been obtained by »new«. Even in the case of heap allocation, one usually uses containers or »make_unique« or »make_shared« instead of »new«, which often absolves one from the responsibility for calling »free«. This also is covered in textbooks. A C++ textbook is: Bjarne Stroustrup, The C++ Programming Language (4th Edition). |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 03 10:29PM >How do I accomplish this: >printf(myStr, "%6.1f", shirtCounts[ii]*100.0/totalShirts); >with streams? #include <iostream> // ::std::cout #include <ostream> // ::std::ostream (<<) #include <iomanip> // ::std::setw, ::std::setprecision #include <ios> // ::std::fixed #include <cmath> // ::std::atan2 #include <cstdlib> // ::std::printf int main() { constexpr double pi = 4 * atan2( 1, 1 ); ::std::cout << ::std::fixed << ::std::setw( 6 )<< ::std::setprecision( 1 ) << pi << "\n"; ::std::printf( "%6.1f\n", pi ); } |
ram@zedat.fu-berlin.de (Stefan Ram): Apr 03 10:58PM > ::std::cout << ::std::fixed << ::std::setw( 6 )<< > ::std::setprecision( 1 ) << pi << "\n"; > ::std::printf( "%6.1f\n", pi ); } Recently, Herb Sutter compared a piece of Python to an equivalent piece of C++ and said that the C++ was not very much longer. I just now become aware of the fact that he omitted the »#include«s from the C++ code! Also, he compared Java and Python as they are now with C++ as it will possibly be when C++ 17 is published (he used concepts in the C++ code). Also, what is really annoying with those manipulators: Some of them change the stream just for the next output operation, while others change the stream forever, and I am not aware of a rule to derive which of the manipulators only modify a stream for the next output operation. And then, some need »#include <iomanip>« and some seem to need »#include <ios>«, and I am also not aware of a rule to determine which require which header. In the case of »printf« the »%6.1f« /always/ only refers to a single output operation and the include /always/ is »#include <cstdio>«. Concepts really would blend good together with a C++doc system, so that we have a standard to express semantic requirements for the operations of a concept. Like, for example, a kind of Doxygen becoming part of ISO C++. |
MikeCopeland <mrc2323@cox.net>: Apr 03 03:22PM -0700 How do I accomplish this: printf(myStr, "%6.1f", shirtCounts[ii]*100.0/totalShirts); with streams? (When I put the expression in an output stream I get e- notation with some values...) TIA --- This email has been checked for viruses by Avast antivirus software. http://www.avast.com |
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 03 06:29PM -0400 On 4/3/2015 6:22 PM, MikeCopeland wrote: > printf(myStr, "%6.1f", shirtCounts[ii]*100.0/totalShirts); > with streams? (When I put the expression in an output stream I get e- > notation with some values...) TIA See iostream manipulators (<iomanip>), like 'std::fixed', 'std::width', 'std::precision', etc. IOW, RTFM. V -- I do not respond to top-posted replies, please don't ask |
"Öö Tiib" <ootiib@hot.ee>: Apr 03 06:11AM -0700 > What do you think of this class? Average class. > ::std::string whatStr; > public: > explicit failure (char const* w) : whatStr(w) {} Can use alternative syntax 'char const s[]' to indicate that C string is expected. > explicit failure (::std::string w) : whatStr(::std::move(w)) {} > ~failure () throw() {} Feels pointless to indicate 'noexcept' here since compiler can see it itself. You can indicate that destructor is default and virtual like: virtual ~failure() = default; However since you do not define neither assignments nor copy-constructions of 'failure' it feels better to remove the destructor as well by rule of zero. > char const* what () const throw() > { return whatStr.c_str(); } Can indicate that member function is virtual override (with keywords 'virtual' and 'override'). > whatStr.append(s); > return *this; > } That looks unneeded overload; its body is identical to previous overload. > whatStr.append(::std::to_string(val)); > return *this; > } Can use: using std::to_string; whatStr.append(to_string(val)); That will consider likely 'to_string's from T's namespace in name lookup. > }; > Some preliminary tests show smaller executable sizes with > this second approach. Technically it is 'catch (std::string const&)' for user? Performance considerations are always less important than usability considerations so I usually derive exception types that may be thrown out of my code from 'std::runtime_error'. Better usability for caller, microscopic difference in performance. If you really need such microscopic performance benefits then better throw enwrapped into 'std::exception' string literals or integers. IOW do not build long texts at throw site. For example: If catch site can't do anything with long text that explains in detail what strange byte sequence did not let a parser to parse the input, then do not build such thing at throw site and executable size is immediately smaller too. |
Azeem Haider <abcloginemail4@gmail.com>: Apr 03 06:00AM -0700 I'm going to upload text file using c++. I'm using this code but this is not working and there is no error in this code. can you give me suggestion why this is not working? Here is code. #include "stdafx.h" #include "iostream" #include "windows.h" #include "wininet.h" #include "tchar.h" #include "iostream" #include "string" #include "sstream" #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Wininet.lib") using namespace std; // Wininet.lib // ws2_32.lib int main(){ TCHAR hdrs[] = _T("Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"); TCHAR frmdata[] = _T("-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"C:\\test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n"); //char frmdata[] = "..."; LPCTSTR accept[] = {_T("*/*"), NULL}; HINTERNET hSession = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hConnect = InternetConnect(hSession, _T("localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("cpp/uploadfile.php"), NULL, NULL, accept, 0, 1); BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata)); //BOOL sent = HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, strlen(frmdata)); if(hSession==NULL) { cout<<"Error: InternetOpen"; } if(hConnect==NULL) { cout<<"Error: InternetConnect"; } if(hRequest==NULL) { cout<<"Error: HttpOpenRequest"; } if(!sent) { cout<<"Error: HttpSendRequest"; } DWORD status; DWORD statusLength = sizeof(status); HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &statusLength, nullptr); if (status != HTTP_STATUS_OK) { cout << "HTTP status: " << status << endl; } //close any valid internet-handles InternetCloseHandle(hSession); InternetCloseHandle(hConnect); InternetCloseHandle(hRequest); return 0; } Here is php file code. <?php move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], "/FileFolder/"); ?> Thanks for your time. |
Victor Bazarov <v.bazarov@comcast.invalid>: Apr 03 09:07AM -0400 On 4/3/2015 9:00 AM, Azeem Haider wrote: > I'm going to upload text file using c++. I'm using this code but this is not working and there is no error in this code. can you give me suggestion why this is not working? > Here is code. > [...] Very little of that code is standard C++. Most of it is Windows-specific, some is VC++ specific. As such they ought to be discussed in the forums dedicated to them (please google for a newsgroup or a web site). Besides, you don't even say what it is "not working", how can we even begin to help you? So, no, sorry, I cannot give you a suggestion *why* it's not working, but here is my suggestion: find a better place to ask OS-specific and compiler-specific questions. V -- I do not respond to top-posted replies, please don't ask |
DeMarcus <demarcus_at_hotmail_com@tellus.orb>: Apr 03 02:07PM +0200 Hi, I just finalized a mock library I've been working on for a couple of weeks. I've tried to use as many C++11 and C++14 features as possible for the best usability but the code could need a couple of fresh eyes looking at it. Can I ask you to give your feedback about what you think? Please be as picky as possible. Even commenting about where the curly braces are put is ok. I also need comments about how easy it is to get started and understand it, and last but not least; what features would you like to see in a good mock library? The library is called Unimock, it's header-only and open source. You find it at github. https://github.com/unimock-cpp/unimock A Hello World example is found in the wiki. https://github.com/unimock-cpp/unimock/wiki Thanks! Daniel |
Geoff <geoff@invalid.invalid>: Apr 02 09:54PM -0700 On Thu, 2 Apr 2015 20:53:36 -0700 (PDT), SpreadTooThin > } >} >what is wrong with this if statement? l.substr(t) compiler doesn't like it. What is the error message from your compiler? Hint: The member substr takes two arguments, what have you given it? |
Barry Schwarz <schwarzb@dqel.com>: Apr 03 12:25AM -0700 On Thu, 02 Apr 2015 21:54:23 -0700, Geoff <geoff@invalid.invalid> wrote: >>what is wrong with this if statement? l.substr(t) compiler doesn't like it. >What is the error message from your compiler? >Hint: The member substr takes two arguments, what have you given it? Both arguments of substr have default values if they are omitted from the function call. -- Remove del for email |
Barry Schwarz <schwarzb@dqel.com>: Apr 03 12:24AM -0700 On Thu, 2 Apr 2015 20:53:36 -0700 (PDT), SpreadTooThin > } >} >what is wrong with this if statement? l.substr(t) compiler doesn't like it. The substr member function returns a value of type string. npos is a value with integer type. What to you expect the comparison operator to do with one argument a string and the other an integer? The first argument of substr must have type size_t. Since thrd has type string, what do you expect substr to do with an argument that cannot be converted to size_t? Did you mean to use some other member function instead of substr, such as find? Telling us the compiler doesn't like something is not nearly as useful as telling us what the diagnostic actually said. Is your cut and paste broken? -- Remove del for email |
Ian Collins <ian-news@hotmail.com>: Apr 03 07:21PM +1300 SpreadTooThin wrote: > } > } > what is wrong with this if statement? l.substr(t) compiler doesn't like it. Your code would be a lot cleaner with modern C++: for( auto longLine : logLines ) { for( auto threadId : threadIds ) { // stuff } } -- Ian Collins |
SpreadTooThin <bjobrien62@gmail.com>: Apr 02 08:53PM -0700 void searchout(void) { string l, t; for (std::vector<string>::iterator it = logLines.begin() ; it != logLines.end(); ++it) { for (std::vector<string>::iterator thrd = threadIds.begin() ; thrd != threadIds.end(); ++thrd) { l = *it; t = *thrd; if (l.substr(t) != string::npos) { // Something is wrong here? } } } } what is wrong with this if statement? l.substr(t) compiler doesn't like it. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment