Viewing 6 reply threads
  • Author
    Posts
    • #10039
      RainerRainer
      Participant
        Up
        1
        Down
        ::
        #include <iostream>
        #include <array>
        #include <vector>
        #include <set>
        #include <unordered_set>
        #include <algorithm>
        #include <iterator>
        
        template<typename C, typename T = typename C::value_type>
            void displayData(const C &container) {
            std::copy(container.cbegin(), container.cend(), std::ostream_iterator<T>(std::cout, " "));
            std::cout << '\n';
        }
        
        int main() {
            const std::array myArray1{-10, 5, 1, 4, 5};
            displayData(myArray1);
        
            const std::array myArray2 = {-10, 5, 1, 4, 5};
            displayData(myArray2);
        
            const std::vector myVector = {-10, 5, 1, 4, 5};
            displayData(myVector);
        
            const std::set mySet = {-10, 5, 1, 4, 5};
            displayData(mySet);
        
            const std::unordered_multiset myUnorderedMultiSet{-10, 5, 1, 4, 5};
            displayData(myUnorderedMultiSet);
        
        return 0;
        }
        
        

        asdfdasfdasfdasf

         

        #include <iostream>
        #include <array>
        #include <vector>
        #include <set>
        #include <unordered_set>
        #include <algorithm>
        #include <iterator>
        template<typename C, typename T = typename C::value_type>
            void displayData(const C &container) {
            std::copy(container.cbegin(), container.cend(), std::ostream_iterator<T>(std::cout, ” “));
            std::cout << \n;
        }
        int main() {
            const std::array myArray1{-10, 5, 1, 4, 5};
            displayData(myArray1);
            const std::array myArray2 = {-10, 5, 1, 4, 5};
            displayData(myArray2);
            const std::vector myVector = {-10, 5, 1, 4, 5};
            displayData(myVector);
            const std::set mySet = {-10, 5, 1, 4, 5};
            displayData(mySet);
            const std::unordered_multiset myUnorderedMultiSet{-10, 5, 1, 4, 5};
            displayData(myUnorderedMultiSet);
        return 0;
        }

        This is an unordered list:

        • First
        • Second
        • Third
        • Test

        This is an ordered list:

        1. First
        2. Second
        3. Third
        4. Test

         

         

        #include <algorithm>
        #include <iostream>
        #include <vector>
        
        constexpr int maxElement() {
            std::vector myVec = {1, 2, 4, 3};
            std::sort(myVec.begin(), myVec.end());
            return myVec.back();
        }
        int main() {
        
            std::cout <<  '\n';
        
            constexpr int maxValue = maxElement();
            std::cout << "maxValue: " << maxValue << '\n';
        
            std::cout << '\n';
        
        }
        
        #include <algorithm>
        #include <iostream>
        #include <vector>
        
        constexpr int maxElement() {
            std::vector myVec = {1, 2, 4, 3};
            std::sort(myVec.begin(), myVec.end());
            return myVec.back();
        }
        int main() {
        
            std::cout <<  '\n';
        
            constexpr int maxValue = maxElement();
            std::cout << "maxValue: " << maxValue << '\n';
        
            std::cout << '\n';
        
        }

        struct MyStruct {
            void constMemberFunction() const {}
            void nonConstMemberFunction() {}
        };
        
        int main() {
        
            const MyStruct myConstStruct;
            MyStruct myNonConstStruct;
        
            myConstStruct.constMemberFunction();
            myNonConstStruct.constMemberFunction();
        
            myConstStruct.nonConstMemberFunction();  // Error: discards const qualifier
            myNonConstStruct.nonConstMemberFunction();
        
        }
        
      • #10062
        AvatarDriton
        Participant
          Up
          0
          Down
          ::

          Sorry, I am having issues showing nice layout code snippet in my comments.

          Copying code from Visual Studio or other applications to here in Editor (in Visual mode) does not show it as expected (as you showed us in last Q&A sessions). For me it shows only the plain text, without any formatting.

          Copying HTML code here in Editor (in Text Mode) shows the code correctly, as long as I am writing in Editor. After submitting my comment, the code loses its layout/formatting.

           

          I saw in your videos a blue bar saying about your account: “Your account has the ability to post unrescricted HTML content”.  Should you, maybe, activate this feature for us also?

           

          Thank you in advance!

           

          Code snippet below already lost its formatting/layout.

           

          <!– HTML generated using hilite.me –>
          <div style=”background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;”>

          <span style="color: #8f5902; font-style: italic;">#include <iostream></span>
          <span style="color: #8f5902; font-style: italic;">#include <array></span>
          <span style="color: #8f5902; font-style: italic;">#include <vector></span>
          <span style="color: #8f5902; font-style: italic;">#include <set></span>
          <span style="color: #8f5902; font-style: italic;">#include <unordered_set></span>
          <span style="color: #8f5902; font-style: italic;">#include <algorithm></span>
          <span style="color: #8f5902; font-style: italic;">#include <iterator></span>
          <span style="color: #204a87; font-weight: bold;">template</span><span style="color: #ce5c00; font-weight: bold;"><</span><span style="color: #204a87; font-weight: bold;">typename</span> <span style="color: #000000;">C</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #204a87; font-weight: bold;">typename</span> <span style="color: #000000;">T</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: #204a87; font-weight: bold;">typename</span> <span style="color: #000000;">C</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">value_type</span><span style="color: #ce5c00; font-weight: bold;">></span>
              <span style="color: #204a87; font-weight: bold;">void</span> <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">C</span> <span style="color: #ce5c00; font-weight: bold;">&</span><span style="color: #000000;">container</span><span style="color: #000000; font-weight: bold;">)</span> <span style="color: #000000; font-weight: bold;">{</span>
              <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">copy</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">container</span><span style="color: #000000; font-weight: bold;">.</span><span style="color: #000000;">cbegin</span><span style="color: #000000; font-weight: bold;">(),</span> <span style="color: #000000;">container</span><span style="color: #000000; font-weight: bold;">.</span><span style="color: #000000;">cend</span><span style="color: #000000; font-weight: bold;">(),</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">ostream_iterator</span><span style="color: #ce5c00; font-weight: bold;"><</span><span style="color: #000000;">T</span><span style="color: #ce5c00; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">cout</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #a40000; border: 1px solid #ef2929;">”</span> <span style="color: #a40000; border: 1px solid #ef2929;">“</span><span style="color: #000000; font-weight: bold;">));</span>
              <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">cout</span> <span style="color: #ce5c00; font-weight: bold;"><<</span> <span style="color: #a40000; border: 1px solid #ef2929;">‘\</span><span style="color: #000000;">n</span><span style="color: #a40000; border: 1px solid #ef2929;">‘</span><span style="color: #000000; font-weight: bold;">;</span>
          <span style="color: #000000; font-weight: bold;">}</span>
          <span style="color: #204a87; font-weight: bold;">int</span> <span style="color: #000000;">main</span><span style="color: #000000; font-weight: bold;">()</span> <span style="color: #000000; font-weight: bold;">{</span>
              <span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">array</span> <span style="color: #000000;">myArray1</span><span style="color: #000000; font-weight: bold;">{</span><span style="color: #ce5c00; font-weight: bold;">-</span><span style="color: #0000cf; font-weight: bold;">10</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">1</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">4</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">};</span>
              <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">myArray1</span><span style="color: #000000; font-weight: bold;">);</span>
              <span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">array</span> <span style="color: #000000;">myArray2</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: #000000; font-weight: bold;">{</span><span style="color: #ce5c00; font-weight: bold;">-</span><span style="color: #0000cf; font-weight: bold;">10</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">1</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">4</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">};</span>
              <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">myArray2</span><span style="color: #000000; font-weight: bold;">);</span>
              <span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">vector</span> <span style="color: #000000;">myVector</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: #000000; font-weight: bold;">{</span><span style="color: #ce5c00; font-weight: bold;">-</span><span style="color: #0000cf; font-weight: bold;">10</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">1</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">4</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">};</span>
              <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">myVector</span><span style="color: #000000; font-weight: bold;">);</span>
              <span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">set</span> <span style="color: #000000;">mySet</span> <span style="color: #ce5c00; font-weight: bold;">=</span> <span style="color: #000000; font-weight: bold;">{</span><span style="color: #ce5c00; font-weight: bold;">-</span><span style="color: #0000cf; font-weight: bold;">10</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">1</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">4</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">};</span>
              <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">mySet</span><span style="color: #000000; font-weight: bold;">);</span>
              <span style="color: #204a87; font-weight: bold;">const</span> <span style="color: #000000;">std</span><span style="color: #ce5c00; font-weight: bold;">::</span><span style="color: #000000;">unordered_multiset</span> <span style="color: #000000;">myUnorderedMultiSet</span><span style="color: #000000; font-weight: bold;">{</span><span style="color: #ce5c00; font-weight: bold;">-</span><span style="color: #0000cf; font-weight: bold;">10</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">1</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">4</span><span style="color: #000000; font-weight: bold;">,</span> <span style="color: #0000cf; font-weight: bold;">5</span><span style="color: #000000; font-weight: bold;">};</span>
              <span style="color: #000000;">displayData</span><span style="color: #000000; font-weight: bold;">(</span><span style="color: #000000;">myUnorderedMultiSet</span><span style="color: #000000; font-weight: bold;">);</span>
           <span style="color: #204a87; font-weight: bold;">return</span> <span style="color: #0000cf; font-weight: bold;">0</span><span style="color: #000000; font-weight: bold;">;</span>
          <span style="color: #000000; font-weight: bold;">}</span>
          

          </div>

        • #10103
          RainerRainer
          Keymaster
            Up
            0
            Down
            ::
            map createTeleBook(const std::string& fileCont){                 
                
                map teleBook; 
                
                std::regex regColon(":");
                
                std::sregex_token_iterator fileContIt(fileCont.begin(), fileCont.end(), regColon, -1);
                const std::sregex_token_iterator fileContEndIt;
                
                std::string entry;
                std::string key;
                int value;
                while (fileContIt != fileContEndIt){                              
                    entry = *fileContIt++;
                    auto comma = entry.find(",");                                  
                    key = entry.substr(0, comma);
                    value = std::stoi(entry.substr(comma + 1, entry.length() -1));
                    teleBook[key] = value;                                         
                }
                return teleBook;
                
            }
            
          • #10114
            AvatarDriton
            Participant
              Up
              0
              Down
              ::

                  // b) check with static_cast and decltype
                  constexpr MyDouble mDoble2(5.5);
                  std::cout << "mDoble2.val: " << mDoble2.getVal() << std::endl;
                  static_assert(std::is_same_v< decltype(mDoble2), const MyDouble >, "Types not the same: mDoble2 and MyDouble");
              
            • #75028
              RainerRainer
              Participant
                Up
                0
                Down
                ::

                #include <iostream>
                
                class Account{
                public:
                  explicit Account(double amount=0.0): balance(amount){}
                
                  void deposit(double amount){
                    balance+= amount;
                  }
                
                  void withdraw(double amount){
                    balance-= amount;
                  }
                  double getBalance() const{
                    return balance;
                  }
                
                private:
                  double balance;
                
                };
                
                
                template <typename T, int N>
                class Array{
                
                public:
                  Array()= default;
                  int getSize() const;
                
                private:
                  T elem[N];
                };
                
                template <typename T, int N>
                int Array<T,N>::getSize() const {
                  return N;
                }
                
                
                int main(){
                
                  std::cout << '\n';
                
                  Array<double,10> doubleArray;
                  std::cout << "doubleArray.getSize(): " << doubleArray.getSize() << '\n';
                
                  Array<Account,1000> accountArray;
                  std::cout << "accountArray.getSize(): " << accountArray.getSize() << '\n';
                
                  std::cout << '\n';
                
                }
                

                constexpr

                int main() {
                }
              • #76789
                RainerRainer
                Keymaster
                  Up
                  0
                  Down
                  ::

                  #include <algorithm>
                  #include <array>
                  #include <deque>
                  #include <iostream>
                  #include <iterator>
                  #include <list>
                  #include <numeric>
                  #include <vector>
                  
                • #76790
                  RainerRainer
                  Participant
                    Up
                    0
                    Down
                    ::

                    #include <algorithm>
                    #include <array>
                    #include <deque>
                    #include <iostream>
                    #include <iterator>
                    #include <list>
                    #include <numeric>
                    #include <vector>
                    
                Viewing 6 reply threads
                • You must be logged in to reply to this topic.