dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2018
    Posts
    36

    question about defining a vector

    bb
    Last edited by Kmilano; April 27th, 2018 at 03:46 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,738

    Re: question about defining a vector

    Instead of this:
    Code:
    std::vector<int> bit{ { 1,2,3,4} };
        bit[1] = 0;
        bit[2] = 0;
        bit[3] = 0;
    why can't you just write:
    Code:
    std::vector<int> bit{1, 0, 0, 0};
    ?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2018
    Posts
    36

    Re: question about defining a vector

    its possible to use binary numbers inside {}, but I'm looking a way to remove the whole {}
    how I can remove it?

  4. #4
    Join Date
    Feb 2017
    Posts
    273

    Re: question about defining a vector

    Quote Originally Posted by Kmilano View Post
    its possible to use binary numbers inside {}, but I'm looking a way to remove the whole {}
    how I can remove it?
    Std::vector has a constructor which allows you to first specify the wanted size of the vector and then also a default value for all index positions. For example if you want the bit vector to have size 4 with a 0 value at all index positions you can do,

    Code:
    std::vector<int> bit(4,0);
    Note that index positions start from 0 so the bit vector can be accessed with indexes 0, 1, 2 and 3. Every other index will be out-of-bounds (unless you resize the vector of course).
    Last edited by wolle; April 28th, 2018 at 03:27 AM.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: question about defining a vector

    Consider

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	constexpr size_t nobits = 4;
    	constexpr size_t nrows = (1 << (nobits - 1)) - 1;
    
    	std::vector<int> bit (nobits, 0);
    
    	for (size_t i = 0; i < nrows; ++i)
    	{
    		for (size_t x = i + 1, a = 1; (x > 0) && (a < nobits); x /=2)
    			bit[a++] = x % 2;
    
    		for (size_t e = nobits - 1; e > 0; --e)
    			cout << "Value syn[" << nobits - e - 1 << "]= " << bit[e] << "     ";
    
    		cout << "     decBits=" << i << endl;
    	}
    }
    which displays

    Code:
    Value syn[0]= 0    Value syn[1]= 0    Value syn[2]= 1         decBits=0
    Value syn[0]= 0    Value syn[1]= 1    Value syn[2]= 0         decBits=1
    Value syn[0]= 0    Value syn[1]= 1    Value syn[2]= 1         decBits=2
    Value syn[0]= 1    Value syn[1]= 0    Value syn[2]= 0         decBits=3
    Value syn[0]= 1    Value syn[1]= 0    Value syn[2]= 1         decBits=4
    Value syn[0]= 1    Value syn[1]= 1    Value syn[2]= 0         decBits=5
    Value syn[0]= 1    Value syn[1]= 1    Value syn[2]= 1         decBits=6
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,144

    Re: question about defining a vector

    Quote Originally Posted by Kmilano View Post
    bb
    Please, remove this meaningless text and restore your original post!
    Otherwise it is absolutely unclear why you have started this thread!
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)